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 的值,它指定图层相当于它父图层的位置, 该值基于父图层的坐标 ...
随机推荐
- js实时监听input中值的变化
$(function(){ $('#inputid').bind('input propertychange', function() { // input 中的值 var params = $(th ...
- 动态加载资源文件(ResourceDictionary)
原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...
- android一些面试题目
1.ListView怎么提高滑动效率 2.说下你做过项目的包的构架,(联网,解析,activity,database) 重点 3.载入大量图片怎么做(包含小图和查看大图) 怎么降低一次跟server的 ...
- HDU 1201
18岁生日 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Just4Fun - Comparaison between const and readonly in C#
/* By Dylan SUN */ Today let us talk about const and readonly. const is considered as compile-time c ...
- 【iOS】使用SQLite与FMDB
iOS中的SQLite与Android中的一模一样,仅仅是调用方法有差异.假设单从调用来讲,Android封装的一套helper更好用一些,而iOS原生的用C语言的几个函数在操作,比較麻烦.只是引入第 ...
- IOT(Index Organized Table)
我们知道一般的表都以堆(heap)的形式来组织的,这是无序的组织方式.Oracle还提供了一种有序的表,它就是索引组织表,简称IOT表.IOT表上必须要有主键,而IOT表本身不对应segment,表里 ...
- 异常Exception in thread "AWT-EventQueue-XX" java.lang.StackOverflowError
今天太背了,bug不断,检查到最后都会发现自己脑残了,粗心写错,更悲剧的是写错的时候还不提示错. 刚才有遇到一个问题,抛了这个异常Exception in thread "AWT-Event ...
- Android自己定义组件系列【5】——高级实践(1)
在接下来的几篇文章将任老师的博文<您可以下拉PinnedHeaderExpandableListView实现>骤来具体实现.来学习一下大神的代码并记录一下. 原文出处:http://blo ...
- Spring、Spring MVC、MyBatis
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Sp ...