iOS设备横屏时,frame和bounds的分别

释放双眼,带上耳机,听听看~!

工程中有两个ViewControllers,其中ViewController是root view controller,底色是红色,上面有一个按钮,点击后加载GreenViewController,并显示其视图,底色是绿色。

首先是ViewController的代码:

#import "ViewController.h"
#import "GreenViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIButton *showGreenViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[showGreenViewBtn setTitle:@"Show Green" forState:UIControlStateNormal];
showGreenViewBtn.frame = CGRectMake(0, 0, 100, 44);
showGreenViewBtn.center = self.view.center;
showGreenViewBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[showGreenViewBtn addTarget:self action:@selector(showGreenView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showGreenViewBtn];
}
- (void)showGreenView:(id)sender {
GreenViewController *greenVC = [GreenViewController new];
[greenVC show];
}
@end

然后是GreenViewController的代码:

#import "GreenViewController.h"
#import "AppDelegate.h"
@interface GreenViewController ()
@end
@implementation GreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor greenColor];
}
- (void)show {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *rootViewController = appDelegate.window.rootViewController;
[rootViewController addChildViewController:self];
[rootViewController.view addSubview:self.view];
NSLog(@"RootViewController");
NSLog(@"f %@", NSStringFromCGRect(rootViewController.view.frame));
NSLog(@"b %@", NSStringFromCGRect(rootViewController.view.bounds));
NSLog(@"GreenViewController");
NSLog(@"f %@", NSStringFromCGRect(self.view.frame));
NSLog(@"b %@", NSStringFromCGRect(self.view.bounds));
[self didMoveToParentViewController:rootViewController];
}
@end

如果是模拟器运行,视图的位置完全正常,因此必须真机运行(模拟器坑死人啊)。横放设备,让红色视图旋转。点击一下show green按钮,结果如下:

 

 

 

各种奇葩。。。

看看控制台的输出:

2014-07-18 10:29:42.754 FrameBoundsRotate[8588:60b] RootViewController
2014-07-18 10:29:42.756 FrameBoundsRotate[8588:60b] f {{0, 0}, {320, 568}}
2014-07-18 10:29:42.757 FrameBoundsRotate[8588:60b] b {{0, 0}, {568, 320}}
2014-07-18 10:29:42.758 FrameBoundsRotate[8588:60b] GreenViewController
2014-07-18 10:29:42.759 FrameBoundsRotate[8588:60b] f {{0, 0}, {320, 568}}
2014-07-18 10:29:42.760 FrameBoundsRotate[8588:60b] b {{0, 0}, {320, 568}}

原来在设备横屏时,RootViewController的视图的frame依然是(0, 0, 320, 568),而bounds则变成了(0, 0, 568, 320)。GreenViewController的视图的frame和bounds都没有变化,由于RootViewController的view的frame没有变化,所以GreenViewController的view的autoresizingMask属性不起作用。

为了解决以上横屏后添加视图时出现的位置变形问题,在show方法中加入self.view.frame = rootViewController.view.bounds,如下:

- (void)show {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *rootViewController = appDelegate.window.rootViewController;
[rootViewController addChildViewController:self];
[rootViewController.view addSubview:self.view];
self.view.frame = rootViewController.view.bounds;
NSLog(@"RootViewController");
NSLog(@"f %@", NSStringFromCGRect(rootViewController.view.frame));
NSLog(@"b %@", NSStringFromCGRect(rootViewController.view.bounds));
NSLog(@"GreenViewController");
NSLog(@"f %@", NSStringFromCGRect(self.view.frame));
NSLog(@"b %@", NSStringFromCGRect(self.view.bounds));
[self didMoveToParentViewController:rootViewController];
}

 

再运行,没问题了:

 

控制台输出:

2014-07-18 10:32:30.320 FrameBoundsRotate[8593:60b] RootViewController
2014-07-18 10:32:30.323 FrameBoundsRotate[8593:60b] f {{0, 0}, {320, 568}}
2014-07-18 10:32:30.324 FrameBoundsRotate[8593:60b] b {{0, 0}, {568, 320}}
2014-07-18 10:32:30.325 FrameBoundsRotate[8593:60b] GreenViewController
2014-07-18 10:32:30.326 FrameBoundsRotate[8593:60b] f {{0, 0}, {568, 320}}
2014-07-18 10:32:30.327 FrameBoundsRotate[8593:60b] b {{0, 0}, {568, 320}}

 

总结:

模拟器坑死人,切记真机调试。

 

Demo地址:点击打开链接

 

人已赞赏
iOS文章

Xcode中文件名旁边的小标记的作用

2020-5-9 20:03:06

iOS文章

iOS 从UITableViewController中分离数据源

2020-5-9 21:18:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索