iOS当该装置是水平屏,frame和bounds分别
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地址:点击打开链接
版权声明:本文博客原创文章,博客,未经同意,不得转载。
iOS当该装置是水平屏,frame和bounds分别的更多相关文章
- IOS 中frame与bounds的区别
文章摘要:http://www.sendong.com/news1733.html bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系 ...
- ios基础之 view的frame 与 bounds 的区别 (转)
前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...
- IOS开发-几种截屏方法
IOS开发-几种截屏方法 1. UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...
- ios view的frame和bounds之区别(位置和大小)
前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...
- iOS View的Frame和bounds之区别,setbounds使用(深入探究)
前言: 在ios开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤其是bound很绕,较难理解. 一.首先,看一下公认的资料: 先看到下面的代码你肯定就明白了一 ...
- ios视图frame和bounds的对比
bounds坐标:自己定义的坐标系统,setbound指明了本视图左上角在该坐标系统中的坐标, 默认值(0,0) frame坐标: 子视图左上角在父视图坐标系统(bounds坐标系统)中的坐标, ...
- iOS 中的frame,bounds,center,transform关联
这里有一篇好文章 http://www.winddisk.com/2012/06/07/transform/ 先看几个知识点,UIView 的frame,bounds,center,transform ...
- 《View Programming Guide for iOS》之frame、bounds和center之间的关系
The frame property contains the frame rectangle, which specifies the size and location of the view i ...
- iOS开发——使用OC篇&frame,bounds,center,position,anchorPoint总结
frame,bounds,center,position,anchorPoint总结 图层的 position 属性是一个 CGPoint 的值,它指定图层相当于它父图层的位置, 该值基于父图层的坐标 ...
随机推荐
- java參数传递方式问题
java的參数传递方式到底是值传递还是引用传递,这一直是一个争论不休的问题,一直以来没有形成统一意见. 在这里,我也仅仅是说一说个人见解,不保证是对的,全当是抛砖引玉. 首先我的观点是java採用的是 ...
- Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的
你是否发现用Texturepacker在打包压缩资源文件之后. 把原稿文件弄丢了,可是又要添加新的小png的时候,却无从下手了,本文就是博主在遇到这个问题后百度了非常多方法,可惜仅仅有plist格式的 ...
- phpmailer【PHP邮件】的用法
第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/ 第二,确认你的服务器系统已经支持socket ,通过phpinfo(); ...
- 在TextView中加入图片
TextView是一个非常强大的控件,有时须要在一个控件中同一时候显示图片和文字,使用TextView非常easy实现. 方法一: 聊天软件比方QQ一般都会有发送表情的功能,使用SpannableSt ...
- hadoop的一些名词解释
在网上收集了一些mapreduce中常用的一些名词的解释,分享一下: Shuffle(洗牌):当第一个map任务完成后,节点可能还要继续执行更多的map 任务,但这时候也开始把map任务的中间输出交换 ...
- POJ3279 Catch That Cow(BFS)
本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...
- 【剑指offer】两个队列实现堆栈
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25076689 题目:用两个队列模拟一个栈,即用两个队列的出队和入队操作.来实现栈的 ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- Net 高效开发
Net 高效开发之不可错过的实用工具 工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存 ...
- [Windows Phone] 地图控制项的经纬度
原文:[Windows Phone] 地图控制项的经纬度 前言 本文主要示范如何使用地图经纬度以及显示地标和行人街道,并透过卷轴控制地图缩放比例的功能. ? 实作 step1 建立专案. ? step ...