user3100783
user3100783

Reputation: 259

Why is this UITextField twice as big as expected?

I am adding a UITextField to my app and it should be 300x30 but it ends up being 600x60. Here is what I have so far... In my app delegate:

- (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;
}

and in 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];
}

inside my OpenGlView's init (I don't think any of this affects the size):

- (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;
}

Why is this UITextField twice as large as it should be?

Upvotes: 1

Views: 141

Answers (3)

Miraslau
Miraslau

Reputation: 548

If your device has retina display, then [[UIScreen mainScreen] bounds] will return two times bigger rectangle. But pixels of UIView and UITextField is still calculated in the other coordinate system. For example, if you have iPhone 4, your rectangle will be {{0,0},{640,960}}. Just divide by 2 both height and width of your mainScreen.bounds rectangle.

UPD: this number 2 comes from [[UIScreen mainScreen] scale] property.

Upvotes: 1

Aditya Aggarwal
Aditya Aggarwal

Reputation: 527

Check autoresiging property for textfield .autoresiging property for width and height for textfield should not vary ,hope it may fix your problem.

Upvotes: 0

thorb65
thorb65

Reputation: 2716

in iOS you define frame rectangles not in pixels but in points. so if you define a frame of 0,0,300,30 it will be layouted on retina display with 0,0,600,60 :-)

Upvotes: 0

Related Questions