2013-12-20 48 views
1

我在我的應用中添加了UITextField,它應該是300x30,但最終爲600x60。以下是我迄今爲止...在我的應用程序委託:爲什麼這個UITextField比預期的大兩倍?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    OpenGLViewController* controller = [[OpenGLViewController alloc] init]; 
    [self.window setRootViewController:controller]; 
    [self.window addSubview:controller.view]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

OpenGLViewController

- (void)loadView { 
    self.view = [[OpenGLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 30)]; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
    textField.font = [UIFont systemFontOfSize:15]; 
    textField.placeholder = @"Search..."; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField.keyboardType = UIKeyboardTypeDefault; 
    textField.returnKeyType = UIReturnKeyDone; 
    textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    textField.returnKeyType = UIReturnKeySearch; 
    textField.delegate = self; 
    [self.view addSubview:textField]; 
} 

裏面我OpenGlView的init(我不認爲任何的這種影響大小):

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) { 
     [self setupLayer]; 
     [self setupContext]; 
     [self setupRenderBuffer]; 
     [self setupFrameBuffer]; 
     [self compileShaders]; 
     glViewport(0, 0, frame.size.width, frame.size.height); 
    } 
    return self; 
} 

爲什麼這UITextField兩倍大,應該是?

+0

出於好奇,如果將其更改爲150X15,它是否會達到您想要的大小? –

回答

1

如果您的設備有視網膜顯示,那麼[[UIScreen mainScreen] bounds]將返回兩倍大的矩形。但UIView和UITextField的像素仍然在其他座標系中計算。例如,如果您有iPhone 4,您的矩形將爲{{0,0},{640,960}}。 只需將您的mainScreen.bounds矩形的高度和寬度除以2即可。

UPD:該數字2來自[[UIScreen mainScreen] scale]屬性。

0

在iOS中,您可以定義不是以像素爲單位,而是以點爲單位的框架矩形。所以如果你定義一個0,0,300,30的幀,它將在視網膜顯示器上用0,0,600,60 :-)

0

檢查文本字段的autoresiging屬性.autoresiging屬性的寬度和高度對於文本字段不應該改變,希望它能解決你的問題。

相關問題