1、edgesForExtendedLayout
從iOS7開始,view controllers默認使用全屏布局(full-screen layout)。同時引進了不少屬性,使你能更自由地控制view controllers如何布局views
通過設置此屬性,可以設定view的邊(上、下、左、右)延伸到整個屏幕。
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
typedef enum : NSUInteger {
UIRectEdgeNone = 0,
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
} UIRectEdge;
edgesForExtendedLayout屬性是enum類型UIRectEdge。默認值是UIRectEdgeAll, 意味著view會被拓展到整個屏幕。比如,當你把一個UIViewControllerpush到一個UINavigationController上:
例子:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
}

由此可見ViewController的view延伸到導航欄和狀態欄里面,如果將self.edgesForExtendedLayout = UIRectEdgeNone;view就不會延伸到導航欄和狀態欄里面,例子:

<p color=redColor> 特別注意:</p>
此屬性僅適用于視圖控制器中嵌入UINavigationController等容器。窗口的根視圖控制器不應對該屬性。這個屬性的默認值是UIRectEdgeAll。
蘋果文檔:
This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
2、automaticallyAdjustsScrollViewInsets
如果view是UIScrollerView或其子類(UITableView)時,設置 self.edgesForExtendedLayout = UIRectEdgeNone;此時UIScrollerView或其子類仍然會被導航欄和狀態欄覆蓋,此時應該設置automaticallyAdjustsScrollViewInsets=YES(默認也是YES).viewController會table頂部添加inset,所以table會出現在navigation bar的底部。但是滾動時又能覆蓋整個屏幕:
例子:
#import "ScrollViewInsetTableViewController.h"
@interface ScrollViewInsetTableViewController ()
@end
@implementation ScrollViewInsetTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"ScrollViewInset";
self.edgesForExtendedLayout=UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets=YES;
}

從蘋果的官方文檔可知,不只是navigation bar,status bar, search bar, navigation bar, toolbar, or tab bar都有類似的效果。
The default value of this property is YES, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to NO if your view controller implementation manages its own scroll view inset adjustments.
3、extendedLayoutIncludesOpaqueBars
extendedLayoutIncludesOpaqueBars是前面兩個屬性的補充。該屬性默認為NO,我們上面討論的edgesForExtendedLayout屬性是在navigationBar/tabBar為透明的情況下的(navigationBar/tabBar默認情況下是透明的),然而在navigationBar/tabBar為不透明時,如果edgesForExtendedLayout 為 UIRectEdgeAll,發現view并沒有延伸到全屏,而是出現在navigationBar與tabBar之間(就像UIRectEdgeNone的效果一樣).
例子:
#import "ViewController.h"
#import "ScrollViewInsetTableViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
// self.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationController.navigationBar.translucent=NO;
}