开发中如果在同一个viewcontroller中,同时出现两个以上的UIListView时,如果包含多个UIListView,设置他们的frame一样,会出现:有一个UIListView是正常的,其他的位置都会偏移到顶部上去,譬如所有的UIListView的frame都设置为(0,0,320,568)的时候,只有一个UIListView的y=0值是在从导航栏底部开始计算,其他的y=0都是从屏幕的最顶部开始计算。要想所有的UIListView的起始位置一致,其他的UIListView的frame必须设置为(0,64,320,568-64),为何?
其实,这是因为iOS7(含)以后,有一个automaticallyAdjustsScrollViewInsets属性引起的,此属性的默认值为YES。
官方文档解释如下:
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
Declaration
SWIFT
var automaticallyAdjustsScrollViewInsets: Bool
OBJECTIVE-C
@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets
Discussion
The default value of this property is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
Availability
Available in iOS 7.0 and later.
简单理解为:automaticallyAdjustsScrollViewInsets是否根据view controller所在界面的status bar,navigation bar,toolbar或者tabbar的高度,自动调整scrollview的 inset。默认为YES自动调整,如果设置为NO,不让view controller自动调整。
所以,只要设置一下view controller的automaticallyAdjustsScrollViewInsets属性为NO,问题就解决了!
self.automaticallyAdjustsScrollViewInsets = NO;