UIView height is not right on iPhone 5

*爱你&永不变心* 提交于 2019-11-30 12:07:55

问题


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.


回答1:


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];




回答2:


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.




回答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.




回答4:


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.



来源:https://stackoverflow.com/questions/12538454/uiview-height-is-not-right-on-iphone-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!