关于iOS屏幕强制旋转的那个问题

何以解忧,唯有努力

前言

今天不是个好日子,为一个屏幕强制旋转搞的天昏地暗...
一开始,我满怀信心的挪了几行代码过来

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == self.preferredInterfaceOrientationForPresentation);
}


- (BOOL) shouldAutorotate
{
    //  (iOS 6)
    //  Only auto rotate if we're on the screen (see above)
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //  (iOS 6)
    //  Prefer (force) landscape
    return UIInterfaceOrientationLandscapeRight;
}

然而,事事尽不如人意,不得行。
于是不得不开始查找资料,然而在网上找了许多资料后,发现几乎都是一样的..跑起来效果也差强人意,话不多说,直接上最后解决方法,是针对push的,present这种比较好实现,这里就不写了。

方案
12.png
  • 先在工程中进行如此配置(工程需要指出竖屏+某些界面的强制横屏)
  • 由于是push,所以我们需要在自定义的NavigationController中进行如下设置,MainViewController为我们需要强制横屏的vc
@interface MyNavigationController ()

@end

@implementation MyNavigationController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([self.topViewController isKindOfClass:[MainViewController class]]){
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}


- (BOOL)shouldAutorotate{
    if ([self.topViewController isKindOfClass:[MainViewController class]]){
        return [self.topViewController shouldAutorotate];
    }
    return NO;
}

@end
  • 在需要选择的vc中添加如下代码
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self orientationToPortrait:UIInterfaceOrientationLandscapeLeft];
}

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

#pragma mark ==强制横屏

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (void)orientationToPortrait:(UIInterfaceOrientation)orientation {
    if([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

//设置为允许旋转
- (BOOL)shouldAutorotate {
    return YES;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

上面supportedInterfaceOrientations方法中的返回值其实也可以设置成你需要的横屏方式,我这设置的只不过还包括了竖屏而已,shouldAutorotate这个是必须要的,不然你在设置横屏的时候将会无效,viewWillTransitionToSize这个函数不是旋转需要加的,我在这里写出来是为了方便在旋转后进行界面UI的调整,size的大小你会发现长宽变成了旋转之前的宽长

  • 如何在返回的时候调整为竖屏呢?
    一开始我是打算在viewWillDisappear中继续调用orientationToPortrait方法,只不过参数为竖屏的值,然而并没有什么用...
    为了解决这个问题,目前我的解决办法就是在push前的页面中也来实现改方法,只不过在viewWillAppear中调用的方法为[self orientationToPortrait:UIInterfaceOrientationPortrait]
    由于时间紧迫就不在仔细去研究了,希望有更好的方案的朋友多多指教~

你可能感兴趣的:(关于iOS屏幕强制旋转的那个问题)