在iOS6中,不推荐使用shouldAutorotateToInterfaceOrientation
。我尝试使用supportedInterfaceOrientations
和shouldAutorotate
使应用程序正常运行以进行自动旋转但失败了。
这个ViewController我不想旋转,但它不起作用。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
有什么想法吗? 感谢您提前提供任何帮助!
答案 0 :(得分:38)
想出来。
1)子类化UINavigationController (层次结构的顶层视图控制器将控制方向。) 确实把它设置为self.window.rootViewController。
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
2)如果您不希望视图控制器旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
3)如果您希望它能够旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
BTW,根据您的需要,另一种相关方法:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
答案 1 :(得分:3)
如果您使用标签栏控制器而不是导航控制器作为根控制器,则需要同样为UITabBarController创建子类。
语法也会有所不同。我成功地使用了以下内容。然后,我在上面想要覆盖的视图控制器上成功使用了上面的示例。在我的情况下,我希望主屏幕不旋转,但我有一个带有电影的常见问题屏幕,我自然希望启用横向视图。工作得很完美!只需注意self.modalViewController的语法更改(如果您尝试使用导航控制器的语法,则会收到编译器警告。)希望这会有所帮助!
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return self.modalViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.modalViewController.supportedInterfaceOrientations;
}