视图控制器处于横向模式,但我从纵向模式获取帧? [英] A view controller is in landscape mode, but I'm getting the frame from portrait mode?

查看:63
本文介绍了视图控制器处于横向模式,但我从纵向模式获取帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有表格视图的设置视图控制器.表视图应该将其框架设置为视图控制器的框架.我的应用只支持横向模式,所以这是我一直使用的方向.

I have a settings view controller with a table view. The table view is supposed to set its frame to be the frame of the view controller. My app only supports landscape mode, so that's the orientation I'm always in.

但是,当设置视图控制器加载时,表格视图具有视图控制器的纵向模式框架,即使它处于横向(360 x 480,但应该是 480 x 360).

However, when the settings view controller loads, the table view has the portrait mode frame of the view controller, even though it's in landscape (360 by 480 when it should be 480 by 360).

此外,当我检查设备是否认为它处于纵向模式时,它返回 true.

Additionally, when I checked to see if the device thought it was in portrait mode, it returned true.

导致问题的原因是什么,我该如何解决?

What is causing the problem and how can I fix it?

推荐答案

当您调用 [UIView init] 时,它将默认为您设备在纵向模式下的屏幕尺寸(iPad 也是如此,但尺寸更大).在 [UIViewController viewDidLoad] 内部,self.view 也将默认为这个大小.

when you call [UIView init] it will default to the screen size of your device in portrait mode (same goes for iPad but with larger size). Inside [UIViewController viewDidLoad] the self.view will also default to this size.

屏幕旋转仅对层次结构中的顶部视图(控制器)进行,并且旋转消息通过支持此行为的容器 VC(例如 UINavigationController、UITabBarController)转发.

Screen rotation is only done to the top view (controller) in your hierarchy and rotation messages are forwarded through container-VCs that support this behavior (e.g. UINavigationController, UITabBarController).

如果您希望视图的大小正确,请使用 [UIView initWithFrame:self.view.bounds] 来创建视图.在 UIViewController 中,您只有机会覆盖 loadView 方法以在调用 viewDidLoad 之前设置框架(或者您可以使用不同的 XIB 进行横向).您还可以通过在 viewDidLoad 中首先设置其框架来硬编码视图的大小

If you want your view to be sized correctly, use [UIView initWithFrame:self.view.bounds] for creating a view. Insinde UIViewController you only have the chance to override the loadView Method to set the frame before viewDidLoad gets called (or you could use a different XIB for landscape). You could also hardcode the view's size by setting its frame first thing in viewDidLoad

这篇关于视图控制器处于横向模式,但我从纵向模式获取帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆