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设备横屏时,frame和bounds的分别,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。