Ios 如何强制视图控制器保持纵向模式?
我有一个带故事板的iOS应用程序。我希望上一个viewcontroller始终处于纵向模式。我一直在阅读,从那以后我就发现了Ios 如何强制视图控制器保持纵向模式?,ios,xcode,rotation,screen-orientation,landscape-portrait,Ios,Xcode,Rotation,Screen Orientation,Landscape Portrait,我有一个带故事板的iOS应用程序。我希望上一个viewcontroller始终处于纵向模式。我一直在阅读,从那以后我就发现了 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 不推荐使用其他方法,如 -(BOOL)shouldAutorotate -(NSInteger)supportedInterfaceOrientations -(UIInterfac
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
不推荐使用其他方法,如
-(BOOL)shouldAutorotate
-(NSInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
但我已经尝试了这么多这种方法的组合,但我没有能够做到这一点。因此,有人能告诉我正确的方法吗?如果在其他UIViewController(即UINavigationController或UIAbbarController)中有UIViewController,则需要将这些消息代理到要实现此行为的子对象
您是否在实现中设置了断点以确保正在查询视图控制器?由于UIViewController嵌入在UINavigationController中,除非您自己转发调用,否则它永远不会被调用。(在我看来,UINavigationController有一点缺陷) 子类UINavigationController如下所示:
@interface RotationAwareNavigationController : UINavigationController
@end
@implementation RotationAwareNavigationController
-(NSUInteger)supportedInterfaceOrientations {
UIViewController *top = self.topViewController;
return top.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate {
UIViewController *top = self.topViewController;
return [top shouldAutorotate];
}
@end
在AppDelegate中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController) {
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
在ViewController中:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
您使用的iOS版本有什么问题?该视图的层次结构是什么?它在UINavigationController,UITabBarController中吗?iOS 6,它在UINavigationController中。我过去解决这个问题的方法是使用
UINavigationController
的子类,并添加一个名为allowRotation
的属性。然后UINavigationController
中包含的每个UIViewControllers
都可以根据需要设置此属性。UINavigationController
子类可以在其shouldAutorotate
方法中简单地返回此属性您是一个摇滚明星!这些年来一直困扰着我。你的解决方案太好了!如果我可以的话,我会不止一次投票给你:-)