問題:
UINavigationController設(shè)置為self.window的根視圖,然后將UIVIewController設(shè)置為UINavigtionController的根控制器忱屑。在UIViewController中加入一個(gè)ScrollView遇八,再ScrollView中加入一個(gè)view惹挟。運(yùn)行發(fā)現(xiàn).:ScrollView顯示正常滔岳,而 ScrollView中的子視圖VIew向下偏移了64個(gè)像素身隐。
解決方法:
設(shè)置UIVIewController的automaticallyAdjustsScrollViewInsets屬性
self.automaticallyAdjustsScrollViewInsets = NO;
在解決這個(gè)問題的過程中邀泉,嘗試了好幾個(gè)屬性的設(shè)置,在此一并記錄一下作用:
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars are translucent by default on 7_0.
1胚迫、首先我們來說說:automaticallyAdjustsScrollViewInsets這個(gè)屬性喷户,默認(rèn)值為YES.
這個(gè)屬性的官方解釋:
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets
Default value 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.
如果是UIScroolview以及繼承自它的控件,默認(rèn)值YES是設(shè)置它的Insets為自適應(yīng)访锻。這里自適應(yīng)其實(shí)就是空出導(dǎo)航欄的位置褪尝。
PS:這個(gè)屬性不僅可以解決類似于開篇的問題。也對直接添加到viewcontroller上的控件起作用期犬。
當(dāng) automaticallyAdjustsScrollViewInsets 為 NO 時(shí)河哑,tableview 是從屏幕的最上邊開始,也就是被導(dǎo)航欄和狀態(tài)欄覆蓋龟虎。如下圖:
當(dāng) automaticallyAdjustsScrollViewInsets 為 YES 時(shí)璃谨,也是默認(rèn)行為,表現(xiàn)就比較正常了鲤妥。
2睬罗、edgesForExtendedLayout
它是一個(gè)類型為UIExtendedEdge的屬性,指定邊緣要延伸的方向旭斥,edgesForExtendedLayout的默認(rèn)值很自然地是UIRectEdgeAll,四周邊緣均延伸古涧,就是說垂券,如果視圖中有navigationBar,下有tabBar羡滑,那么視圖仍會延伸覆蓋到四周的區(qū)域菇爪。
PS:這個(gè)屬性只對直接添加到viewcontroller上的控件都起作用,但不適用于開篇那樣的問題柒昏。
edgesForExtendedLayout設(shè)置成UIRectEdgeNone截圖如下:
導(dǎo)航欄就變成灰色的了凳宙,此時(shí)只需設(shè)置一下
self.navigationController.navigationBar.translucent = NO;
顯示就完全正常了。edgesForExtendedLayout 為UIRectEdgeNone時(shí)职祷,效果跟automaticallyAdjustsScrollViewInsets 為 YES 時(shí)一樣氏涩。
3届囚、extendedLayoutIncludesOpaqueBars
英文解釋:
So, if you extend your view to cover the navigation bar (edgesForExtendedLayout toUIRectEdgeAll) and the parameter is NO (default) it wont cover the status bar if it's opaque.
If something is not clear, write a comment and I'll answer to it.
- How iOS knows what UIScrollView to use? *iOS grabs the first subview in your viewcontroller's view, so the one at index 0, and if it's a subclass ofUIScrollView then applies the explained properties to it.
Of course, this means that UITableViewController works by default (since theUITableView is the first view).
默認(rèn)值為NO,這個(gè)屬性在狀態(tài)欄不透明的情況下才生效是尖。如果狀態(tài)欄是不透明的意系,那么頁面的布局默認(rèn)是不會包含狀態(tài)欄的,除非將這個(gè)屬性設(shè)置成為YES饺汹。
但是我怎么設(shè)置效果都不明顯蛔添,有待進(jìn)一步研究。