project那里有两个ViewControllers。间ViewController它是root view controller,红色背景,有一个顶button,点击加载后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 greenbutton,结果例如以下:
各种奇葩。。。
看看控制台的输出:
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地址:点击打开链接
版权声明:本文博客原创文章,博客,未经同意,不得转载。