iOS-屏幕旋转

2017-01-14 19:52:53来源:CSDN作者:Galen_liang人点击

环境-开发环境 Xcode8.0 iOS 7.0 适配iPad/iPhone/导航栏

只有iPhone某个VC需要切换横竖屏,其他页面都是默认选项

实现思路:
- 整个Device旋转
- 只旋转某个View

1.AppDelegate.h 配置

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{    if (IS_IPHONE_DEVICE) { //当是iPhone        if (self.window.rootViewController.presentedViewController != nil) {        //  限定所有present的模态视图只能竖屏            return UIInterfaceOrientationMaskPortrait;        }        QWTBC *tbc = (QWTBC *)self.window.rootViewController;        QWBaseNC *baseNC = (QWBaseNC *)tbc.selectedViewController;        if ([baseNC.topViewController isKindOfClass:[XXXXVC class]]) {            return UIInterfaceOrientationMaskAllButUpsideDown;        } else {            return UIInterfaceOrientationMaskPortrait;        }    }    else { //iPad         return UIInterfaceOrientationMaskAll;    }}

2.BaseVC(所有UIViewController 的父类)

// 全局设置所有控制器只能在某个方向- (BOOL)shouldAutorotate{    return YES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    if (IS_IPHONE_DEVICE) {        return UIInterfaceOrientationMaskPortrait;    }    else {        return UIInterfaceOrientationMaskAll;    }}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return [super preferredInterfaceOrientationForPresentation];}

3.BaseNC(所有UINavigationController 的父类)

- (BOOL)shouldAutorotate{    return self.visibleViewController.shouldAutorotate;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{   return self.visibleViewController.supportedInterfaceOrientations;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return self.visibleViewController.preferredInterfaceOrientationForPresentation;}

4.在某个需要旋转的界面

#pragma mark - 屏幕旋转- (void)rotationToDeviceOrientation {    if (需要横屏) {        [self forceLandScape];    }    else { //需要竖屏        [self forcePortrait];    }    [UIViewController attemptRotationToDeviceOrientation];}- (void)forcePortrait {    CGFloat width = UISCREEN_WIDTH;    CGFloat height = UISCREEN_HEIGHT;    if (width > height) {        UIDevice *device = [UIDevice currentDevice];        NSNumber *number = @(UIDeviceOrientationPortrait);        [device setValue:number forKey:@"orientation"];    }}-(void)forceLandScape{    CGFloat width = UISCREEN_WIDTH;    CGFloat height = UISCREEN_HEIGHT;    if (width < height) {        UIDevice *device = [UIDevice currentDevice];        NSNumber *number = @(UIDeviceOrientationLandscapeLeft); //特别注意UIDeviceOrientationLandscapeLeft         [device setValue:number forKey:@"orientation"];    }}- (UIInterfaceOrientationMask)supportedInterfaceOrientations {    if (IS_IPHONE_DEVICE) {        if (当前是竖屏) {            return UIInterfaceOrientationMaskLandscapeRight;        } else {            return UIInterfaceOrientationMaskPortrait;        }    }    else {        return UIInterfaceOrientationMaskAll;    }}//在Button的点击事件中调用-rotationToDeviceOrientation 旋转屏幕核心方法 [device setValue:number forKey:@"orientation"];        [UIViewController attemptRotationToDeviceOrientation];解释:这个方法不是私有API。 这是不安全的使用API。 你不使用任何私有方法,而是使用“内部事物”。 如果Apple更改“orientation”的值,它会失败。最少我上线没有被拒过常见的误区:  请务必看清楚这些方向,这其实是不同的三个枚举.UIDevice 你最好选择的是UIDeviceOrientation 里面的枚举typedef NS_ENUM(NSInteger, UIDeviceOrientation) {    UIDeviceOrientationUnknown,    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left    UIDeviceOrientationFaceUp,              // Device oriented flat, face up    UIDeviceOrientationFaceDown             // Device oriented flat, face down} __TVOS_PROHIBITED;typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {     UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),} __TVOS_PROHIBITED;typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft} __TVOS_PROHIBITED;

2.旋转View

//旋转屏幕,但是只旋转当前的View      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];      self.view.transform = CGAffineTransformMakeRotation(M_PI/2);      CGRect frame = [UIScreen mainScreen].applicationFrame;      self.view.bounds = CGRectMake(0, 0, frame.size.height, 320);  

假若里面需要键盘操作的话 会发现非常奇怪,网上有解决方案 但是我没试过
http://blog.lessfun.com/blog/2014/09/24/ios8-issue-keyboard-orientation-and-idletimerdisabled-not-working/

 [self adjustViewFrameToFullScreen];    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];    //改设备方向    if (IsIOS8) {        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];    }

查阅的相关资料
http://www.jianshu.com/p/787283fb4f6a
http://foggry.com/blog/2014/08/08/ping-mu-xuan-zhuan-xue-xi-bi-ji/ 经典
https://gold.xitu.io/entry/57a2b9f979bc440054a3d738 经典
http://webcache.googleusercontent.com/search?q=cache:http://blog.lessfun.com/blog/2014/09/24/ios8-issue-keyboard-orientation-and-idletimerdisabled-not-working&gws_rd=cr&ei=vkNrWJfGJMGQsgGAqb6wDg

View 旋转
http://blog.csdn.net/zhenyu5211314/article/details/24003809

关于私有API
http://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8/28253241

http://blog.csdn.net/Galen_liang/article/details/54427451

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台