Objective C iOS 9锁定viewController纵向方向 [英] Objective C iOS 9 lock viewController portrait orientation

查看:90
本文介绍了Objective C iOS 9锁定viewController纵向方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级我的项目后,请在iOS 9.2上执行旧代码,使之不再使用

After upgrade my project do iOS 9.2 my old code become obsolete

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

在收到旋转通知视图集旋转后,我发现了这个丑陋的解决方案(并用动画绘制了我的视图控制器):

And I found this ugly solution, after receive a rotation notification view set rotation (And repaint with animation my view controller):

-(void)didRotate:(NSNotification *)notification
{

        [UIView animateWithDuration:0 animations:^{
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        }];       
 }

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

}

-(void)viewWillDisappear:(BOOL)animated
{
        [super viewWillDisappear:YES];

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

如何仅在此ViewController中锁定方向?

How can I lock orientation only in this ViewController?

推荐答案

对于iOS 9及更高版本

  • 首先确定您的基本视图控制器
  • 假设它是导航控制器.

导航控制器的属性:

@property (strong, nonatomic) UINavigationController *mainNavigationController;

将以上内容替换为:

@property (strong, nonatomic) MainNavigationContrller *mainNavigationController;

您的MainNavigationContrller.m如下所示:

Your MainNavigationContrller.m will look like the below:

@interface MainNavigationContrller ()

@end

@implementation MainNavigationContrller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if(IPHONE)
    {
        UIViewController *topVC = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).yourNavigationController.topViewController;
        if([topVC isKindOfClass:[yourViewController class]])
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    }

    return UIInterfaceOrientationMaskAll;
}

有了这个,您不需要删除info.plist条目,我们可以仅针对特定视图阻止方向.

With this, you need not to remove your info.plist entries and we can block the orientation for specific view only.

这对我有用.

这篇关于Objective C iOS 9锁定viewController纵向方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆