博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS当该装置是水平屏,frame和bounds分别
阅读量:6887 次
发布时间:2019-06-27

本文共 4344 字,大约阅读时间需要 14 分钟。

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] RootViewController2014-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] GreenViewController2014-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] RootViewController2014-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] GreenViewController2014-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之UI--小实例项目--添加商品和商品名(使用xib文件终结版) + xib相关知识点总结...
查看>>
小知识~让你的DLL类库带上注释
查看>>
Junit测试打印详细的log日志,可以看到sql
查看>>
还是畅通工程
查看>>
什么是Monad?
查看>>
vue实现数据驱动视图原理
查看>>
BeanFactory 简介以及它 和FactoryBean的区别(阿里面试)
查看>>
SpringBoot 全局统一记录日志
查看>>
mysql数据统计技巧备忘录
查看>>
Functor and Monad in Swift
查看>>
linux调试工具glibc的演示分析-core dump double free【转】
查看>>
Top 22 Free Responsive HTML5 Admin & Dashboard Templates 2018
查看>>
适合初学者的python实际例子
查看>>
我的第一个python web开发框架(25)——定制ORM(一)
查看>>
Android padding 和margin
查看>>
IOS UIView 01-View开始深入 绘制像素到屏幕上
查看>>
在Android中使用Protocol Buffers(中篇)
查看>>
Apache之Rewrite和RewriteRule规则梳理以及http强转https的配置总结
查看>>
Python3+pyshark捕获数据包并保存为文件
查看>>