【问题标题】:Auto Rotation iOS 8 Navigation Controller自动旋转 iOS 8 导航控制器
【发布时间】:2015-08-30 22:23:08
【问题描述】:

我有某些viewControllers 由UINavigationController 管理(推送和弹出)。我想将不同的viewControllers 限制为不同的orientations,比如第一个应该只在Portrait,第二个在portrait,第三个在landscape,第四个可以是portrait 和landscape。我从storyBoard 在isInitialViewController 上设置了ViewController,

- (BOOL) shouldAutorotate{
return NO;

}

工作没有任何问题,但是当我从storyBoard 将navigation controller(通过推送和弹出管理这四个视图)设置为isInitialViewController 时,此函数停止被调用,现在autoratates。如何使用 UINavigationController 作为 isInitialViewController 停止 autorotating 这些视图。我使用以下函数取决于它是哪个ViewController

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}

- (BOOL) shouldAutorotate{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
//    return UIInterfaceOrientationLandscapeLeft |
//    UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}

【问题讨论】:

    标签: objective-c iphone ios8 uinavigationcontroller autorotate


    【解决方案1】:

    只需继承 UINavigationController 并覆盖适当的方法:

    .h 文件:

    @interface CustomUINavigationController : UINavigationController
    @property   BOOL canRotate;
    @end
    

    .m 文件:

    @implementation CustomUINavigationController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    
    - (BOOL)shouldAutorotate
    {
        return self.canRotate;
    }
    @end
    

    【讨论】:

    • 谢谢,答案是正确的,但我只是注意到我的情况有点不同,所以我在我的 SlideView 中实现了这些功能并且它有效.. 非常感谢 :)跨度>
    【解决方案2】:

    如果您为 UINavigation Controller 和 Override 界面方向方法创建 Objective C 类别会怎样...我想您尝试控制 Autorotation。

    Objective C 类别 http://rypress.com/tutorials/objective-c/categories

    【讨论】:

    • 非常简单快速的实现ShouldAutorotate方法,并尝试控制自转。
    • 为 UINavigationController 开发一个类别
    • 谢谢你的想法,但我只是注意到我的场景有点不同,所以我在 SlideView 中实现了这些功能,它工作了。非常感谢 :)
    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多