IOS6转屏处理

栏目: IOS · 发布时间: 7年前

内容简介:以前的 iOS 程式 (4.0 版以前) 都是用 addSubview 来将 app 的 root view 加到 window 上面,例如下面的作法:我发现在 iOS 6 上面,这个作法会使 rotation 失效 (iOS 6 以前完全没问题),必须改成这个方式:iOS 4 之后才支持self.window.rootViewController = self.viewController;的方式。

以前的 iOS 程式 (4.0 版以前) 都是用 addSubview 来将 app 的 root view 加到 window 上面,例如下面的作法:

[self.window addSubview:self.viewController.view];

我发现在 iOS 6 上面,这个作法会使 rotation 失效 (iOS 6 以前完全没问题),必须改成这个方式:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
    self.window.rootViewController = self.viewController;
} else {
    [self.window addSubview:self.viewController.view];
}

iOS 4 之后才支持self.window.rootViewController = self.viewController;的方式。

另外,相关的ViewController中,如果支持转屏需要添加如下CODE:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);
}

- (BOOL)shouldAutorotate {
  return YES;
}

说明:如果你想禁止转屏,可以返回NO

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

如果VIEW需要处理转屏,需要添加如下代码处理:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

view会自动调整宽和高 要么重写view的 - (void) layoutSubviews 方法,调整view,添加对应的处理。推荐前者方法,后者方法除非在转屏时会修改重新调整某些UI的精确位置时使用


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Math Adventures with Python

Math Adventures with Python

Peter Farrell / No Starch Press / 2018-11-13 / GBP 24.99

Learn math by getting creative with code! Use the Python programming language to transform learning high school-level math topics like algebra, geometry, trigonometry, and calculus! In Math Adventu......一起来看看 《Math Adventures with Python》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具