我需要在控制器中的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
}
}
viewDidLoad
中进行布局。在这里,你可以设置数据模型和初始化视图,但是所有的布局都应该在viewWillLayoutSubviews
中完成,因为self.view.frame
将准确地表示当前的框架,考虑到旋转。 - Aaron Brager