13

I am migrating my apps over and on one I use a UIPickerView. In the viewDidLoad method I create the picker and set it's y origin to be self.view.frame.size.height so that it is off screen. Then I just move it up when needed.

On the iPhone 5 self.view.frame.size.height is still returning 480 yet the UIView fills the screen correctly.

I've even tried using CGRectGetHeight(self.view.bounds) thinking that it may return something different...no dice. Any ideas as to why this maybe occurring.

random
  • 8,568
  • 12
  • 50
  • 85
  • 2
    Add a 640x1136 pixel Default-568h@2x.png file to your project. Without it, your app will not stretch to full height on the new devices. More info [here](http://stackoverflow.com/a/12397309/877465). – Filip Radelic Sep 21 '12 at 21:35
  • Do other views show full screen? did you remember to add an iPhone 5 screenshot to your target? – Moshe Gottlieb Sep 21 '12 at 21:35
  • Yes I have the new default image in there. I found the issue. My nib has to be set to "iPhone 5 full screen" (I think that's what it's called). I guess that it loads the view size from the nib and doesn't take into account autoResizeMask. – random Sep 21 '12 at 22:03
  • That should not make any difference, view controller's view gets the size based on where it's being added and not before viewWillAppear: The nib settings is only for presentation within Interface Builder, that's why it's under "simulated metrics". – Filip Radelic Sep 21 '12 at 22:13

4 Answers4

22

That is because the size you selected in the view's nib will be used until viewWillAppear: (BOOL) animated method. Then it will take the correct size.

However you can use the following code to have the correct size since viewDidLoad is called:

CGSize viewSize = [[UIScreen mainScreen] bounds].size;
viewSize = CGSizeMake(viewSize.width, viewSize.height - STATUS_BAR_HEIGHT);

STATUS_BAR_HEIGHT is 20 but it depends on your app. You may or may not need to add that line.

EDIT

The problem with using mainScreen bounds is that the frame doesn't change on orientation change. That is the way it is designed. You can work it out with the following:

    CGSize viewSize = [[UIScreen mainScreen] bounds].size;

    if(UIInterfaceOrientationIsLandscape(CURRENT_ORIENTATION)){
        viewSize = CGSizeMake(viewSize.height, viewSize.width - STATUS_BAR_HEIGHT);

    } else {
        viewSize = CGSizeMake(viewSize.width, viewSize.height - STATUS_BAR_HEIGHT);
    }

CURRENT_ORIENTATION is [[UIApplication sharedApplication] statusBarOrientation];

Gerald
  • 567
  • 1
  • 10
  • 17
htafoya
  • 18,261
  • 11
  • 80
  • 104
18

I've been dealing with the same issue. I tried getting the views frame in - (void)viewDidLoad but discovered that it's not updated until - (void)viewWillAppear:(BOOL)animated gets called.

So try getting the views frame in - (void)viewWillAppear:(BOOL)animated and you should be alright.

aspartame
  • 4,622
  • 7
  • 36
  • 39
  • 2
    This is great! However, I don't want to keep changing the view every time it's about to appear. It only needs to change once the view is loaded. Hmm ... there must be some other happy middle ground we can find (a different method, p'raps?). – Joe D'Andrea Oct 09 '12 at 19:10
  • @JoeD'Andrea I have the same issue, i think that the fastest workaround is to add a isFirstLoad flag to use in the viewWillAppear method. – htafoya Jan 14 '13 at 15:35
  • @JoeD'Andrea you can also use [[UIScreen mainScreen] bounds].size, that will work – htafoya Jan 14 '13 at 16:20
3

It looks like iOS detects the app is designed for use on iPhone 5 after detecting a Default-568h@2x.png image in your bundle. How to develop or migrate apps for iPhone 5 screen resolution? has a complete answer outlining the entire process.

Community
  • 1
  • 1
Ravi
  • 7,929
  • 6
  • 38
  • 48
1

I found the issue. My nib has to be set to "iPhone 5 full screen" (I think that's what it's called). I guess that it loads the view size from the nib and doesn't take into account autoResizeMask.

When the view size in IB is set to "Size: None" the view is not sized right. It has to be set to "Size: Retina 4 Full Screen". Strange.

random
  • 8,568
  • 12
  • 50
  • 85
  • but if you do that and run your app on an lets say iphone 4 you will get the incorrect view height cause you get the hight of an iphone 5 view. – BObereder Sep 28 '12 at 07:31