iOS横竖屏切换

导航控制器PUSH

需求:当前竖屏下情况下,导航栏直接PUSH出一个横屏的控制器,并在POP后返回竖屏状态。
效果图:

push.gif

主要实现是通过下面这两步:
①. 让AppDelegate在屏幕方向相关的代理方法中,返回我们期望的屏幕方向

// AppDelegate.h中
typedef NS_ENUM(NSUInteger, ScreenOrientation) {
    PortraitMode = 0,
    LandscapeMode,
    AllButUpsideDownMode,
};

@property (nonatomic, assign) ScreenOrientation viewRotateMode;

// AppDelegate.m中
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    
    if (self.viewRotateMode == 0) {
        return UIInterfaceOrientationMaskPortrait;
    } else if (self.viewRotateMode == 1) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.viewRotateMode == 0) {
        return UIInterfaceOrientationMaskPortrait;
    } else if (self.viewRotateMode == 1) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

②. 通过KVC改变UIDeviceorientation属性

NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

具体实现:
在被push的ViewController中:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
    delegate.viewRotateMode = LandscapeMode;
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
    delegate.viewRotateMode = PortraitMode;
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

模态,单个控制器交互后横竖屏切换

一、重载根控制器

@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations
在这个属性的文档中有如下解释:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. 

iOS 6之后,控制器的方向由根控制器管理,可使用继承或者分类的方式,重载根控制器关于旋转屏幕的三个方法,将方向的选择权归还给子控制器。

@implementation UINavigationController (manualRotate)

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

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

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

@end


@implementation UITabBarController (manualRotate)
//在`iOS 5`及更早的版本中默认返回`NO`, 其他默认返回`YES`
- (BOOL)shouldAutorotate {
    return self.selectedViewController.shouldAutorotate;
}

// shouldAutorotate为Yes时才会被调用
// iPad默认返回UIInterfaceOrientationMaskAll
// iPhone默认返回UIInterfaceOrientationMaskAllButUpsideDown
// 此方法的返回值与AppDelegate的application:supportedInterfaceOrientationsForWindow:的交集才是有效值
// AppDelegate的application:supportedInterfaceOrientationsForWindow:会覆盖工程配置中(Info.plist)的Supported interface orientations
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.selectedViewController.supportedInterfaceOrientations;
}

// 系统会默认按statusBar的方向来展示一个新的控制器,即默认按当前方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}

@end

至此,新的控制器,只需要重载上面的方法就能管理自己的方向了。

二、强制修改设备方向

@implementation UIDevice (manualRotate)
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[self currentDevice]];
    NSInteger val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}
@end

三、使用

  1. 父控制器为竖屏,子控制器为横屏,只需要在自控制器重写三个方法即可。注意需要控制好父、子控制器之间方向的关系。

  2. 同一个控制器,需要在竖屏和横屏之间切换,如带有视频播放的控制器。需要重写上面的方法,然后调用[UIDevice setOrientation: orientation] 方法进行屏幕旋转。

    这里写图片描述

四、布局

同一控制器在横竖屏之间切换,经常需要不同的布局。在iOS 8及以后的系统可以使用SizeClass方便地部署横竖屏的AutoLayout。在Xcode 8及更高版本后,界面有所改变。

这里写图片描述

所有不是在Vary for Traits 下添加的约束会被当做为所有尺寸的设备添加的约束。
点击Vary for Traits 选择宽高,可以为指定的设备(群)添加约束。

五、方向说明

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;

UIInterfaceOrientation 用于表示界面的方向,当设备向右旋转时,UI需要向左旋转,所以在LeftRight两个方向上,UIInterfaceOrientationUIDeviceOrientation是相反的。

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
}

UIInterfaceOrientationMask ,在单个方向上与UIInterfaceOrientation并无区别,只是多了UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown等表示界面方向的集合。

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),
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容