iOS – 将特定的UIViewController锁定到特定方向

前端之家收集整理的这篇文章主要介绍了iOS – 将特定的UIViewController锁定到特定方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序支持每4个方向,我有一个在LandscapeRight中的UIViewController.
我正在使用UINavigationController来推送UIViewController,我希望UIViewController仅在UIInterfaceOrientationLandscapeRight中,但是当我旋转手机时,它会切换回其他方向.
-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationLandscapeRight;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

解决方法

只需删除那些shouldAutorotate,supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation.

并将此添加到您想要仅显示横向的viewcontroller.

-(void)viewDidAppear:(BOOL)animated{

    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
                                forKey:@"orientation"];
}

实际上,这是来自类似问题的解决方案.
How to force view controller orientation in iOS 8?

原文链接:https://www.f2er.com/iOS/335181.html

猜你在找的iOS相关文章