【发布时间】:2014-06-10 12:50:21
【问题描述】:
我有仅纵向的菜单和可以是纵向或横向的详细视图(带有UIWebView)。
当我进入详细视图并旋转设备横向并以这种方式从该屏幕返回到仅应为纵向的菜单时,菜单处于横向(以及状态栏和导航栏)。
有没有办法避免这种情况并强制屏幕旋转到所需(支持的)方向?
【问题讨论】:
我有仅纵向的菜单和可以是纵向或横向的详细视图(带有UIWebView)。
当我进入详细视图并旋转设备横向并以这种方式从该屏幕返回到仅应为纵向的菜单时,菜单处于横向(以及状态栏和导航栏)。
有没有办法避免这种情况并强制屏幕旋转到所需(支持的)方向?
【问题讨论】:
@kkumpavat 提出的解决方案对我不起作用,或者不够清楚,我无法理解。我做了更多搜索并找到了使用的解决方案:
// http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
// http://openradar.appspot.com/radar?id=697
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
withObject:UIInterfaceOrientationPortrait];
但这会产生烦人的警告,我已将其替换为:
//force update of screen orientation
if (self.interfaceOrientation != [self preferredInterfaceOrientationForPresentation]) {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: [self preferredInterfaceOrientationForPresentation]]
forKey:@"orientation"];
}
从 NavigationController 的 viewDidAppear 调用。
【讨论】:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; Apple 提供了两个相似但非常不同的不同枚举:UIDeviceOrientation 和 UIInterfaceOrientation。设备属性是只读的,因为你不能强制物理设备改变方向! statusBarOrientation 是 可写的,并且会做你试图用 setValue:forKey: 做的事情
将菜单viewController的viewWillAppear改成如下
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIViewController *controller = [[UIViewController alloc] init];
[self presentViewController:controller animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
}
【讨论】: