xcode 模态视图控制器在 iOS 6 中强制横向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12060534/
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
Modal View Controller force Landscape orientation in iOS 6
提问by Darren
I have a UITabBarController presented in Portrait mode. On one of the tabs I have a button that shows a UIViewController modally (A simple storyboard segue performs the action).
我有一个以纵向模式显示的 UITabBarController。在其中一个选项卡上,我有一个按钮以模态方式显示 UIViewController(一个简单的故事板 segue 执行操作)。
I want this modal view to be shown in Landscape mode, but I can't get it to turn automatically.
我希望此模式视图以横向模式显示,但我无法让它自动转动。
I have this in the modal views controller
我在模态视图控制器中有这个
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
I've added landscapeLeft to the .plist supported orientations (although this also allows the TabBar to be rotated which I don't want)
我已将 LandscapeLeft 添加到 .plist 支持的方向(尽管这也允许旋转我不想要的 TabBar)
I've also added this to the ViewDidLoad of the modal view
我还将它添加到模式视图的 ViewDidLoad
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
but I just can't get it to rotate by itself.
但我无法让它自行旋转。
Thanks
谢谢
EDIT ----
编辑 - -
It seems shouldAutoRotate isn't even being called!
似乎甚至没有调用 shouldAutoRotate !
Also, I'm trying to detect the orientation and this code below always shows 2, regardless of orientation!
另外,我正在尝试检测方向,无论方向如何,下面的代码始终显示 2!
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait) {
NSLog(@"1");
self.hostView.frame = CGRectMake(0, 60, 200, 255);
} else {
NSLog(@"2");
self.hostView.frame = CGRectMake(0, 45, 480, 255);
}
EDIT 2 ---
编辑 2 ---
My bad. I guess I should have mentioned I was using iOS 6. The display rotates automatically on iOS 5. shouldAutorotateToInterfaceOrientation is deprecated so I need to read up on the new methods.
我的错。我想我应该提到我使用的是 iOS 6。显示在 iOS 5 上自动旋转。不推荐使用 shouldAutorotateToInterfaceOrientation,所以我需要阅读新方法。
采纳答案by Esmond
iOS 6.0 Release Notes
"More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. "
iOS 6.0 发行说明
“更多的责任正在转移到应用程序和应用程序委托。现在,iOS 容器(例如 UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转。”
-(BOOL)shouldAutorotate
of UIViewController under any Container (like UINavigationController) will not be called from IOS6.
-(BOOL)shouldAutorotate
任何容器(如 UINavigationController)下的 UIViewController 都不会从 IOS6 调用。
Tried a solution with using Category to add method to existing UINavigationController Class.
尝试使用 Category 将方法添加到现有 UINavigationController 类的解决方案。
inside the shouldAutorotatemethod, you can call shouldAutorotate method in each view controller of self.viewControllers. In fact, you can pass to your child view controller.
在shouldAutorotate方法中,你可以在self.viewControllers 的每个视图控制器中调用shouldAutorotate 方法。事实上,您可以传递给您的子视图控制器。
回答by miham
If you want to force rotation in iOS6, your rootviewController should implement these methods:
如果你想在 iOS6 中强制旋转,你的 rootviewController 应该实现这些方法:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return YES;
}
回答by CoderDan
In iOS 6 it seems to be this method may help force a specific orientation on iOS 6.
在 iOS 6 中,这种方法似乎有助于在 iOS 6 上强制特定方向。
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
I am still trying to get it to work and will update with any findings.
我仍在努力让它工作,并将更新任何发现。
回答by CSmith
I do exactly this in my app, and do it via shouldAutoRotateToInterfaceOrientation, slightly differently than you:
我在我的应用程序中正是这样做的,并且通过 shouldAutoRotateToInterfaceOrientation 来完成,与您略有不同:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
return (YES);
return (NO);
}
for iOS 6, you must also set window.rootViewController = {your root view controller}; in your app delegate. I'm hoping this is your issue
对于 iOS 6,您还必须设置 window.rootViewController = {your root view controller}; 在您的应用程序委托中。我希望这是你的问题