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 的值,它指定图层相当于它父图层的位置, 该值基于父图层的坐标 ...
随机推荐
- Kdd Cup 2013 总结2
- 设计模式六大原则-OCP
开闭原则(Open Closed Principle)是Java世界里最基础的设计原则,它指导我们如何建立一个稳定的.灵活的系统. 定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. S ...
- Android 一些错误
android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...
- HDU 1114 Piggy-Bank 全然背包
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式
简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...
- Android分屏显示LogCat
Eclipse里有非常多界面组件,文件列表.编辑区.类结构等等,在这么多界面组件里,再打开一个Logcat就基本没有什么空间了.与其挤在一起还不如分开成两个窗体. 或者你有两个屏幕,想一个屏幕编辑,一 ...
- Windows Phone开发(8):关于导航的小技巧
原文:Windows Phone开发(8):关于导航的小技巧 前文用几个例子对导航做了简单介绍,在一般应用中,使用上一篇文章中说到的方法,其实也够用了,不过,为了能够处理一些特殊的情况,有几个小技巧还 ...
- java大全经典的书面采访
果学网 -专注IT在线www.prismcollege.com 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面.以便更充分地注意与当前目标有关的方面.抽象并 ...
- Mono+CentOS+Jexus
在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验准备工作 本篇文章采用Mono+CentOS+Jexus的方式实现部署.Net的Web应用程序(实战,上线项目). ...
- DevExpress控件使用之RichEditControl的使用
原文:DevExpress控件使用之RichEditControl的使用 做Winform的,我们一般都知道,传统.NET界面有一个RichTextBox控件,这个是一个富文本控件,可以存储图片文字等 ...