UIView高度在iPhone 5上不正确 [英] UIView height is not right on iPhone 5
问题描述
我正在一次又一次地使用UIPickerView
迁移我的应用程序.在viewDidLoad方法中,我创建选择器并将其y原点设置为self.view.frame.size.height
,使其不在屏幕上.然后我只在需要时将其向上移动.
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.
在iPhone 5上,self.view.frame.size.height
仍返回480,但UIView
正确填充屏幕.
On the iPhone 5 self.view.frame.size.height
is still returning 480 yet the UIView
fills the screen correctly.
我什至尝试使用CGRectGetHeight(self.view.bounds)
认为它可能会返回不同的结果……没有骰子.关于为什么会发生这种情况的任何想法.
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.
推荐答案
这是因为在viewWillAppear: (BOOL) animated
方法之前,将使用在视图的笔尖中选择的大小.然后它将采用正确的大小.
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.
但是,由于调用了viewDidLoad
,因此您可以使用以下代码来具有正确的大小:
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
是20,但这取决于您的应用程序.您可能需要也可能不需要添加该行.
STATUS_BAR_HEIGHT
is 20 but it depends on your app. You may or may not need to add that line.
编辑
使用mainScreen
边界的问题是,框架在方向改变时不会改变.这就是它的设计方式.您可以使用以下方法解决此问题:
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
是[[UIApplication sharedApplication] statusBarOrientation];
这篇关于UIView高度在iPhone 5上不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!