ios实现屏幕旋转的方法

1、屏蔽AppDelegate下面的屏幕旋转方法

#pragma mark - 屏幕旋转的
//-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {
// return UIInterfaceOrientationMaskPortrait;
//}````

2、对UINavigationController和UITabBarController写两个扩展类别,此东西不需要在具体的ViewController中引用
UINavigationController+Autorotate.h

````//
//  UINavigationController+Autorotate.h
//  fitmiss
//
//  Created by bill on 15/12/16.
//  Copyright © 2015年 lear. All rights reserved.
//

#import 

@interface UINavigationController (Autorotate)

@end````


UINavigationController+Autorotate.m

````//
//  UINavigationController+Autorotate.m
//  fitmiss
//
//  Created by bill on 15/12/16.
//  Copyright © 2015年 lear. All rights reserved.
//

#import "UINavigationController+Autorotate.h"


@implementation UINavigationController (Autorotate)

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end````

UITabBarController+Autorotate.h

````//
//  UITabBarController+Autorotate.h
//  fitmiss
//
//  Created by bill on 15/12/16.
//  Copyright © 2015年 lear. All rights reserved.
//

#import 

@interface UITabBarController (Autorotate)

@end````

UITabBarController+Autorotate.m

````//
//  UITabBarController+Autorotate.m
//  fitmiss
//
//  Created by bill on 15/12/16.
//  Copyright © 2015年 lear. All rights reserved.
//

#import "UITabBarController+Autorotate.h"

@implementation UITabBarController (Autorotate)

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

@end````

3.在使用的ViewController中的再次重写这三个方法,可以在根ViewController中重写如下方法,就能实现竖屏,子ViewController再重写实现旋转

  • (BOOL)shouldAutorotate {
    //是否允许转屏
    return NO;
    }
  • (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    //viewController所支持的全部旋转方向
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
  • (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ //viewController初始显示的方向
    return UIInterfaceOrientationPortrait;
    }

注意:在使用的时候发现在ios9以下的版本出现一个奇怪的问题,就是编译出来的app默认是横的,解决办法是看app.plist下面Supported interface orientations里的列表中,正屏的是不是排在第一个。

[转自:http://www.cnblogs.com/lear/p/5051818.html](http://www.cnblogs.com/lear/p/5051818.html)

你可能感兴趣的:(ios实现屏幕旋转的方法)