【发布时间】:2013-11-11 11:53:21
【问题描述】:
我需要在控制器中运行 viewDidLoad 方法之前检测设备方向。我尝试了针对其他问题的所有解决方案,但我无法解决。
在 xCode 5 和 iOS 7 中使用 SotryBoard 的最佳方式是什么?
提前感谢您!
编辑:实际上我正在使用
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
switch ([[UIDevice currentDevice] orientation]) {
case (UIDeviceOrientationPortrait):
// landscape layout
break;
default:
// portrait layout
break;
}
但它不起作用......
已解决:使用
- (void)viewDidLoad {
[super viewDidLoad];
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
// Portrait
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
// Landscape
}
}
【问题讨论】:
-
不要混淆 UIDeviceOrientation 和 UIInterfaceOrientation
-
您不应该在
viewDidLoad中进行布局。在这里您可以设置数据模型和初始化视图,但您的所有布局都应该在viewWillLayoutSubviews中完成,其中self.view.frame将准确地表示当前帧,并考虑旋转。 -
它是否也适用于storyBoard?
标签: ios orientation device uiinterfaceorientation