0

I have a UITextView that should consume the height of the screen, minus the height of the nav bar and keyboard (as when the view loads the keyboard appears). Obviously on a 4 inch device this means the text view should be slightly taller. Is it possible in Interface Builder to make the height device dependent?

If not, can I do it in code? Is it possible to do without some Auto Layout constraints?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

4 Answers4

2

It would be very easy to do it by using Auto Layout constraints. Is there any special reason for not using it?

Billy Kan
  • 110
  • 1
  • 8
  • No, I was just curious if there was an easy way. I assume I adjust detect the device in code and adjust the constant accordingly? – Doug Smith Oct 06 '13 at 14:33
0

You need to do this check in your code, You can use below Macor to check the device:

#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE

then:

 if (IS_IPHONE5 )
 {
       //textView.frame = ..
 }
 else
 {
       //textView.frame = ..
 }

And yes you can do it using a couple of Auto Layout constraints which is the best practice to do that, check this SO question:

iPhone 4 & 5 Autoresize Vertical Space

Community
  • 1
  • 1
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
0

you can use struts and springs. but auto layout is the new hotness.

iOS AutoLayout vs Springs & Struts

Community
  • 1
  • 1
madmik3
  • 6,975
  • 3
  • 38
  • 60
0

Going off null's answer:

Im assuming you are placing this UITextView inside a UIViewController?

You can set the frame for the UITextView at runtime.

In viewDidLoad:

-(void)viewDidLoad {

    UITextView *textView = [[UITextView alloc] init];

    //216 is the height of the keyboard, 20 is the height of the status bar
    if (isiPhone5) {
        CGRect frame = CGRectMake(0, self.navigationBar.frame.size.height + 20, self.frame.size.width, 568-216); 
        [textView setFrame:frame];
    }
    else {
        CGRect frame = CGRectMake(0, self.navigationBar.frame.size.height + 20, self.frame.size.width, 480-216); 
        [textView setFrame:frame];
    }


}
Rohan Panchal
  • 1,211
  • 1
  • 11
  • 28