iOS 6 旋转问题 - 呈现的模态视图控制器没有旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12554204/
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
iOS 6 Rotation issue - No rotation from Presented Modal View Controller
提问by ryryan
I have a MainViewController which has a button which pushes a new view (InfoViewController), via flip horizontailly. like so:
我有一个 MainViewController,它有一个按钮,通过水平翻转来推送新视图(InfoViewController)。像这样:
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
The MainView Controller supports Portrait and PortraitUpsideDown. Like so:
MainView 控制器支持 Portrait 和 PortraitUpsideDown。像这样:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait |
UIInterfaceOrientationMaskPortraitUpsideDown);
}
In my InfoViewController it also states the above code. In my AppDelegate it has this in the LaunchOptions:
在我的 InfoViewController 中,它还说明了上述代码。在我的 AppDelegate 中,它在 LaunchOptions 中有这个:
[self.window setRootViewController:self.mainViewController];
In my app.plist file it supports all orientations. This is because other views need to support landscape as well. So On my MainViewController and InfoViewController I need only Portrait and PortraitUpsideDown. But on another view I need all orintations.
在我的 app.plist 文件中,它支持所有方向。这是因为其他视图也需要支持横向。所以在我的 MainViewController 和 InfoViewController 上,我只需要 Portrait 和 PortraitUpsideDown。但从另一个角度来看,我需要所有的定位。
My MainViewController works fine, but my InfoViewController is working for all orientations.
我的 MainViewController 工作正常,但我的 InfoViewController 适用于所有方向。
I am having extreme diffulty trying to get this to work in iOS6. I have researched other posts and tried the assistance other people have provided, but had no luck whatsoever. Please can someone help me acheive this thank you. And I'm a Objective-C newbie :p
我在尝试让它在 iOS6 中工作时遇到了极大的困难。我研究了其他帖子并尝试了其他人提供的帮助,但没有任何运气。请有人帮我实现这个谢谢。我是一个 Objective-C 新手 :p
回答by Sverrisson
Don′t support all orientations in your app plist file, only those that your root view controller supports.
不支持应用 plist 文件中的所有方向,只支持根视图控制器支持的方向。
Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation:
method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow:
and shouldAutorotate
methods:
Autorotation 在 iOS 6 中发生了变化。在 iOS 6 中,shouldAutorotateToInterfaceOrientation:
不推荐使用 UIViewController的方法。取而代之的是,您应该使用supportedInterfaceOrientationsForWindow:
和shouldAutorotate
方法:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Modal ViewControllers no longer get rotation calls in iOS 6:
The willRotateToInterfaceOrientation:duration:,
willAnimateRotationToInterfaceOrientation:duration:,
and
didRotateFromInterfaceOrientation:
methods are no longer called on any view controller that makes a full-screen presentation over
itself—for example those that are called with: presentViewController:animated:completion:
.
在 iOS 6 中,Modal ViewControllers 不再获得旋转调用:willRotateToInterfaceOrientation:duration:,
willAnimateRotationToInterfaceOrientation:duration:,
和
didRotateFromInterfaceOrientation:
方法不再在任何对自身进行全屏演示的视图控制器上调用 - 例如那些使用: 调用的视图控制器presentViewController:animated:completion:
。
You can let the view controller that presents your modal view controller inform it of rotation.
Also, now you use: presentViewController:animated:completion:
to present the view controller. presentModalViewController:animated:
is deprecated which you use in the code.
您可以让呈现您的模态视图控制器的视图控制器通知它旋转。此外,现在您使用:presentViewController:animated:completion:
来呈现视图控制器。presentModalViewController:animated:
您在代码中使用的已弃用。
回答by Denis Kutlubaev
I have solved similar problems, while using tab bar controller.
我在使用标签栏控制器时解决了类似的问题。
Subclass UITabBarController. Implement these methods:
子类 UITabBarController。实现这些方法:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
NSLog(@"Orientation:%d", [[UIDevice currentDevice] orientation]);
for (UIViewController *viewController in self.viewControllers) {
[viewController shouldAutorotate];
}
return YES;
}
If you want to handle rotations in controllers inside tabbarcontroller, in each of the controllers in the tab bar controller implement those methods too and write code to handle orientation change. If you don't want to handle it, then you don't need to implement those methods. TabBarControllers methods will always run when orientation changes. Even twice for unknown reason.
如果要处理 tabbarcontroller 中控制器的旋转,则在 tab bar controller 中的每个控制器中也实现这些方法并编写代码来处理方向变化。如果你不想处理它,那么你不需要实现这些方法。TabBarControllers 方法将始终在方向更改时运行。甚至两次不明原因。
Yes, and don't forget to delete all shouldAutorotate methods. I moved to the new orientation model completely. If you want to make them remain, probably, it will be harder.
是的,不要忘记删除所有 shouldAutorotate 方法。我完全转向了新的定位模型。如果你想让它们留下来,可能会更难。
回答by Alok SInha
Make a category by subclassing UINavigationController and implement following methodes in .h file
通过继承 UINavigationController 创建一个类别,并在 .h 文件中实现以下方法
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
in .m file
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
and implement following methodes in the view controller class ,class u want to enable rotation
并在视图控制器类中实现以下方法,类你想启用旋转
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
回答by Luter Rinding
add this code on Subclass UITabBarController .m
在子类 UITabBarController .m 上添加此代码
@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
@end
@implementation NameClassUITabBar
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
@end
Here I've posted my solution/experince in tab bar controller with rotations: http://luterr.blogspot.sg/2015/04/example-code-uiinterfaceorientationmask.html
在这里,我在带有旋转的标签栏控制器中发布了我的解决方案/经验:http: //luterr.blogspot.sg/2015/04/example-code-uiinterfaceorientationmask.html