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 <UIKit/UIKit.h>

@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 <UIKit/UIKit.h>

@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中重写如下方法,就能实现竖屏

- (BOOL)shouldAutorotate
{
    return NO;
}

摘自网上网友的回复内容,如下:

以下方法仅对deploy target大于等于iOS6的工程有效,如果题主的应用需要支持iOS5(默哀),请pass。

  • 在info.plist中设置方向,包含你需要的所有方向,以题中意,UpSideDown和LandScapeLeft;
  • 继承UITabBarController,override以下三个方法
 - (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

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

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
  • 继承UINavigationController,override和UITabBarController中相同的方法,将selectedViewController改为topViewController
 - (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

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

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
  • 在真正实现界面的ViewController里,override上面这三个方法,override规则如下:
    preferredInterfaceOrientationForPresentation表示viewController初始显示时的方向;
    supportedInterfaceOrientations是在该viewController中支持的所有方向;
    shouldAutorotate表示是否允许旋屏。

流程说明
首先,对于任意一个viewController,iOS会以info.plist中的设置和当前viewController的preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations三者支持的方法做一个交运算,若交集不为空,则以preferredInterfaceOrientationForPresentation为初始方向,交集中的所有方向均支持,但仅在shouldAutorotate返回YES时,允许从初始方向旋转至其他方向。若交集为空,进入viewController时即crash,错误信息中会提示交集为空。
其次,UINavigationController稍有些特别,难以用常规API做到同一个naviVC中的ViewController在不同方向间自如地切换。(如果去SO之类的地方搜索,会找到一个present empty viewController and then dismiss it之类的hacky trick,不太建议使用),如果要在横竖屏间切换,建议使用presentXXX方法。
再次,AppDelegate中有一个委托方法可以动态的设置应用支持的旋转方向,且此委托的返回值会覆盖info.plist中的固定设置。使用该方法的便利之处不言自明,但缺点是搞明白当前哪个ViewController即将要被显示,很可能会导致耦合增加;
最后,以上均为个人在iOS8 SDK下得到的实践结果,请题主结合工程实际参考使用。

时间: 2024-08-07 09:49:10

ios实现屏幕旋转的方法的相关文章

IOS UIDevice & IOS检测屏幕旋转实例

一 UIDevice 简介 UIDevice类提供了一个单例实例代表当前的设备.从这个实例中可以获得的信息设备,比如操作系统名称.电池电量值(batteryLevel).电池状态(batteryState).设备的类型(model,比如iPod.iPhone等).设备的系统(systemVersion) 二 获取 UIDevice 实例 通过[UIDevice currentDevice]可以获取这个单粒对象 UIDevice *device = [UIDevice currentDevice]

当rootViewController为tabbarController时,控制屏幕旋转的方法

在ios6以后,ios系统改变了屏幕旋转的方法,如果要设置屏幕旋转的方法,需要在rootvc里面进行编写,例如 UIViewController *viewCtrl = [[UIViewController alloc] init]; UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; if ([window respondsToSelect

iOS实现屏幕旋转

iOS实现屏幕旋转有两种方式 1. 应用本身支持 2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘.导航.UIAlertView) 如果你只是要把竖屏的播放器,做成支持横屏的,没有其他界面操作, 就可以考虑用第二种方式去做,比较简单 ,不过要注意计算view Frame 这两种方式看你具体的使用场景了,具体场景选择合适的方式. 公司项目中有几个界面要支持横竖屏,(直播录制界面.直播观看界面.视频回看界面). 刚开始我想着用第二种方式去解决,但是我们视频录制.观看界面有

【iOS】屏幕旋转,屏幕自适应方向变化

1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: Objective-c代码   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 2. 这个函数时用来确定我们的应用所支持的旋转方向.如果想要支持每个方向则直接返回YES就行,还可以单独判断某一方向: Objective-c代码   - (BO

Android应用不随手机屏幕旋转的方法

在主配置文件里面.在需要设置的activity项后面加上 android:screenOrientation="portrait",这个activity就保持竖屏显示了:在每个activity项后面加上android:screenOrientation="portrait",整个应用就保持竖屏显示.

iOS屏幕旋转总结

一.屏幕旋转检测方法 在特别的场景下,需要针对屏幕旋转作特殊处理.在ios系统下实现相关的功能还是比较方便的. 我下面介绍两种方法: 1.监测状态栏方向 1 /** 2 * 当手机屏幕发生左右横屏变化时,我们需根据此时的设备方向做出相应的调整 3 * 例如,在ViewController中,我们需要监控手机屏幕是否转动了,需要在通知中心中注册通知 4 */ 5 - (void)viewWillAppear:(BOOL)animated{ 6 [super viewWillAppear:anima

ios(ipad,iphone)屏幕旋转检测通用方法

在特别的场景下,需要针对屏幕旋转作特殊处理.在ios系统下实现相关的功能还是比较方便的. 我下面介绍两种方法: 1.注册UIApplicationDidChangeStatusBarOrientationNotification通知(举例:在一个viewcontroller类的viewdidload中注册该通知),示例代码如下: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarO

iOS屏幕旋转方向的相关方法

在iOS应用开发过程中,经常会遇到设置屏幕方向,或者根据屏幕方向改变界面的时候,所以现在就来说一下屏幕方向的那些事情. 关于方向,经常会遇到以下的两个对象: 1.UIDeviceOrientation(机器设备的方向) ================================== UIDeviceOrientationUnknown //未知方向 UIDeviceOrientationPortrait, //设备直立,home按钮在下 UIDeviceOrientationPortrai

iOS学习笔记(3)— 屏幕旋转

iOS学习笔记(3)— 屏幕旋转 一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate通知当前的主Window. 3.window通知它的rootViewController. 4.rootViewController判断所支持的旋转方向,完成旋转. iOS系统中屏幕旋转事件没有像触碰事件那样进行hitTest,所以只有rootViewControl