ios开发UIView自适应问题
2015-12-04 10:49
585 查看
http://blog.csdn.net/ouyangtianhan/article/details/17282209
我的UIViewController的Xib里面的View是 3.5寸的即(320*480)的。
在设置自适应自动伸长以后,经常会在ViewDidLoad方法里面初始化一些界面,这时候使用主self.view.frame时,发现frame和xib里面的尺寸是一样的,并没有自适应伸长,结果很多subview错位了,上网查了下,在stackoverflow上发现一段说明:
The frame is not guaranteed to be the same in viewDidLoad as it will be when the view is eventually displayed. UIKit adjusts the frame of your view controller's view prior to
displaying it, based on the context in which will appear. The size is determined based on interface orientation and the dimensions of any visible navigation bar, tab bar, toolbar, or status bar (which itself has a height that can change, e.g. when you're
on a phone call).
It helps to understand what happens when a view controller's view is loaded and displayed:
Something accesses your view controller's
for the first time. This may occur in your own code, or in UIKit in response to a user action like selecting a tab.
UIKit lazy-loads your view controller's view by calling
it's defined, or by loading the view from the NIB that was specified in
If neither exists, UIKit just loads an empty view.
UIKit calls
the view and its subviews have been fully loaded. At this point the view's frame will be whatever it was
set to in the NIB, or in
Something calls for UIKit to display your view controller's view. Again, this may be a user action like tapping on a tab, or an explicit method call in your code like
UIKit resizes the view based on the context in which it will be presented, as described above.
UIKit calls
frame should now be the size that will be displayed.
UIKit displays the view, with or without animations.
UIKit calls
As you can see, if you need to know the size of your view's frame before it gets presented,
your one and only opportunity. Just remember that this size may change after the view appears for various reasons, including rotation events or changes in status bar height. For this reason, it's important to give every subview an appropriate autoresizingMask,
to ensure that the layout can adjust itself properly for any change in bounds.
If you wish to build your view hierarchy manually, the recommended place to do so is in loadView. Since you construct the view yourself in this method, you can initialize its frame to whatever you'd like. The size you choose doesn't matter much, since UIKit
is likely to change it on you anyway. Just make sure you set your autoresizingMasks appropriately.
其实代码是没错的,不管是在xib里面设置自适应,还是在ViewDidLoad方法里卖弄设置,得到的self.view.frame始终是和xib里面的尺寸是一样的,这时你就郁闷了,因为很多subView的尺寸要根据self.view的尺寸来设置。
所以对subView的一些适应性设置,不能在Viewdidload里面,而是需要在viewWillAppear和viewDidAppear里面完成,在这两个方法里面self.view.frame是自适应之后的值。
自己认为最好的方式,还是对subview设置好合适autoresizingMasks。
我的UIViewController的Xib里面的View是 3.5寸的即(320*480)的。
在设置自适应自动伸长以后,经常会在ViewDidLoad方法里面初始化一些界面,这时候使用主self.view.frame时,发现frame和xib里面的尺寸是一样的,并没有自适应伸长,结果很多subview错位了,上网查了下,在stackoverflow上发现一段说明:
The frame is not guaranteed to be the same in viewDidLoad as it will be when the view is eventually displayed. UIKit adjusts the frame of your view controller's view prior to
displaying it, based on the context in which will appear. The size is determined based on interface orientation and the dimensions of any visible navigation bar, tab bar, toolbar, or status bar (which itself has a height that can change, e.g. when you're
on a phone call).
It helps to understand what happens when a view controller's view is loaded and displayed:
Something accesses your view controller's
viewproperty
for the first time. This may occur in your own code, or in UIKit in response to a user action like selecting a tab.
UIKit lazy-loads your view controller's view by calling
loadViewif
it's defined, or by loading the view from the NIB that was specified in
initWithNibName:bundle:.
If neither exists, UIKit just loads an empty view.
UIKit calls
viewDidLoadonce
the view and its subviews have been fully loaded. At this point the view's frame will be whatever it was
set to in the NIB, or in
loadView.
Something calls for UIKit to display your view controller's view. Again, this may be a user action like tapping on a tab, or an explicit method call in your code like
pushViewController:animated:or
presentModalViewController:animated:.
UIKit resizes the view based on the context in which it will be presented, as described above.
UIKit calls
viewWillAppear:. The
frame should now be the size that will be displayed.
UIKit displays the view, with or without animations.
UIKit calls
viewDidAppear:.
As you can see, if you need to know the size of your view's frame before it gets presented,
viewWillAppear:is
your one and only opportunity. Just remember that this size may change after the view appears for various reasons, including rotation events or changes in status bar height. For this reason, it's important to give every subview an appropriate autoresizingMask,
to ensure that the layout can adjust itself properly for any change in bounds.
If you wish to build your view hierarchy manually, the recommended place to do so is in loadView. Since you construct the view yourself in this method, you can initialize its frame to whatever you'd like. The size you choose doesn't matter much, since UIKit
is likely to change it on you anyway. Just make sure you set your autoresizingMasks appropriately.
其实代码是没错的,不管是在xib里面设置自适应,还是在ViewDidLoad方法里卖弄设置,得到的self.view.frame始终是和xib里面的尺寸是一样的,这时你就郁闷了,因为很多subView的尺寸要根据self.view的尺寸来设置。
所以对subView的一些适应性设置,不能在Viewdidload里面,而是需要在viewWillAppear和viewDidAppear里面完成,在这两个方法里面self.view.frame是自适应之后的值。
自己认为最好的方式,还是对subview设置好合适autoresizingMasks。
相关文章推荐
- easyui 动态生成datagrid列
- JS之模板技术(aui / artTemplate)
- 如何限制一个UITextField的输入长度
- easyui--validate验证
- UEFI的两种启动模式
- 在Legacy BIOS与UEFI 两种模式安装Windows 8操作系统的方法
- ZOJ 3911 Prime Query (线段树区间更新)
- Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分
- 设计模式之建造者模式(Builder)
- UITableView UICollectionView经验积累
- UEFI模式安装Win7旗舰版系统的方法
- UEFI模式安装win8,让开机速度快如闪电!
- Qt GUI程序中主线程与非主线程之间的通信
- Tips:取消UICollectionView的隐式动画
- UEFI安全启动怎么关闭 关闭UEFI启动项的方法图解
- UEFI怎么用 UEFI安全启动设置添加方法步骤图解
- 《apue》读书笔记 停止更新
- SQL Report Builder 报表里面的常见问题分析
- uefi启动是什么意思 UEFI启动对比Bios启动优势在哪里
- 限制UITextField 输入字数