xcode iPhone 5 上的 UIView 高度不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12538454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:34:06  来源:igfitidea点击:

UIView height is not right on iPhone 5

objective-cxcodeuiviewcgrect

提问by random

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.heightso that it is off screen. Then I just move it up when needed.

我正在反复迁移我的应用程序,我使用UIPickerView. 在 viewDidLoad 方法中,我创建了选择器并将它的 y 原点设置self.view.frame.size.height为离开屏幕。然后我只是在需要时将其向上移动。

On the iPhone 5 self.view.frame.size.heightis still returning 480 yet the UIViewfills the screen correctly.

在 iPhone 5self.view.frame.size.height上仍然返回 480 但UIView正确填充屏幕。

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.

我什至尝试使用CGRectGetHeight(self.view.bounds)认为它可能会返回不同的东西......没有骰子。关于为什么会发生这种情况的任何想法。

回答by htafoya

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

那是因为您在视图的笔尖中选择的大小将在方法之前使用viewWillAppear: (BOOL) animated。然后它将采用正确的尺寸。

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

但是,您可以使用以下代码来获得正确的大小,因为viewDidLoad被调用:

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

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

STATUS_BAR_HEIGHT是 20 但这取决于您的应用程序。您可能需要也可能不需要添加该行。

EDIT

编辑

The problem with using mainScreenbounds 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:

使用mainScreen边界的问题是框架不会随着方向改变而改变。这就是它的设计方式。您可以通过以下方式解决:

    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_ORIENTATIONis [[UIApplication sharedApplication] statusBarOrientation];

CURRENT_ORIENTATION[[UIApplication sharedApplication] statusBarOrientation];

回答by aspartame

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

我一直在处理同样的问题。我尝试获取视图框架,- (void)viewDidLoad但发现它在- (void)viewWillAppear:(BOOL)animated被调用之前不会更新。

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

所以试着把视图框- (void)viewWillAppear:(BOOL)animated放进去,你应该没问题。

回答by Ravi

It looks like iOS detects the app is designed for use on iPhone 5 after detecting a [email protected] image in your bundle. How to develop or migrate apps for iPhone 5 screen resolution?has a complete answer outlining the entire process.

在您的捆绑包中检测到 [email protected] 图像后,iOS 似乎检测到该应用程序专为在 iPhone 5 上使用而设计。如何为 iPhone 5 屏幕分辨率开发或迁移应用程序?有一个完整的答案,概述了整个过程。

回答by random

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.

我发现了这个问题。我的笔尖必须设置为“iPhone 5 full screen”(我认为这就是所谓的)。我猜它从笔尖加载视图大小并且没有考虑 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.

当 IB 中的视图大小设置为“大小:无”时,视图的大小不正确。它必须设置为“尺寸:Retina 4 全屏”。奇怪的。