從iOS7開始,控制器就默認(rèn)添加了全屏屬性,因此,你就有更多的方式去操作view的布局,具體用到的屬性包括:
- edgesForExtendedLayout
- automaticallyAdjustsScrollViewInsets
- extendedLayoutIncludesOpaqueBars
edgesForExtendedLayout
我們可以根據(jù)以上屬性設(shè)置view的鋪滿樣式。
想象一下,默認(rèn)情況下,我們從一個普通的UIViewController跳轉(zhuǎn)到一個UINavigationController,view默認(rèn)的展示樣式是從導(dǎo)航欄底部開始。
但是你可以通過設(shè)置edgesForExtendedLayout
為不同類型來控制view的樣式(top, left, bottom, right)。
可以看一下例子:
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor redColor];
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
你沒有設(shè)置edgesForExtendedLayout
的值,其默認(rèn)的值是UIRectEdgeAll
,所以view是延伸到整個屏幕的高度。
效果如下圖:

如你所見,紅色背景延伸到了狀態(tài)欄(status bar)下面。
假若你將 edgesForExtendedLayout
的值設(shè)置為UIRectEdgeNone
,意味著你告訴view不要講其擴(kuò)展到整個屏幕。
其效果如下:
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor redColor];
viewController.edgesForExtendedLayout = UIRectEdgeNone;
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

關(guān)于另外一個屬性automaticallyAdjustsScrollViewInsets
.
這個屬性屬于UIScrollView或包含UIScrollView的控制器(比如UITableView繼承自UIScrollView,UIWebView中也包含UIScrollView)。
如果你想要你的view從導(dǎo)航欄底部開始,但是在滑動時,讓其穿透到導(dǎo)航欄的底部。
在這種情況下,如果你將edgesForExtendedLayout
設(shè)置為UIRectEdgeNone
,雖然可以從導(dǎo)航欄底部開始,但滑動時無法穿透到導(dǎo)航欄底部。
怎么辦呢?
這時候就顯示出automaticallyAdjustsScrollViewInsets
的作用了。
如果你將edgesForExtendedLayout
的值設(shè)置為UIRectEdgeAll
,automaticallyAdjustsScrollViewInsets
設(shè)置為YES(edgesForExtendedLayout
默認(rèn)為UIRectEdgeAll
,automaticallyAdjustsScrollViewInsets
默認(rèn)就是YES),就能實現(xiàn)上述需求。
具體如下圖:

上圖是將
edgesForExtendedLayout
設(shè)置為UIRectEdgeAll
,automaticallyAdjustsScrollViewInsets
默認(rèn)就是NO的情況)。
下圖是將edgesForExtendedLayout
設(shè)置為UIRectEdgeAll
,automaticallyAdjustsScrollViewInsets
默認(rèn)就是YES的情況)(也就是系統(tǒng)默認(rèn)情況)

關(guān)于另外一個屬性。
字面意思是:是否延伸到包含不透明的狀態(tài)欄。
-
extendedLayoutIncludesOpaqueBars
這個值是一個補(bǔ)充,默認(rèn)值是NO;
默認(rèn)的蘋果的狀態(tài)欄(status bar)是透明的。如果狀態(tài)欄不透明,這個試圖就不回擴(kuò)展到不透明的狀態(tài)欄底部,除非將其值設(shè)置為YES
This value is just an addition to the previous ones. If the status bar is opaque, the views won't be extended to include the status bar too, unless this parameter is YES.
所以如果狀態(tài)欄不透明,即使你設(shè)置edgesForExtendedLayout
為UIRectEdgeAll
,extendedLayoutIncludesOpaqueBars
為NO(默認(rèn)如此),view不會延伸到狀態(tài)欄底部的。
怎么判斷UIScrollView在使用?
iOS會抓取控制器view的第一個子視圖,(也就是index = 0),如果是UIScrollView或者UIScrollView的子類,就可以使用上文描述的屬性。
如果視圖是普通的UIView,可以添加一個線來解決。
self.navigationController.navigationBar.translucent = NO;