iOS 7以后在ViewController里面引進(jìn)了一系列屬性用于管理頁面布局。 extendedLayout有幾個(gè)相似的參數(shù):
edgesForExtendedLayout
automaticallyAdjustsScrollViewInsets
extendedLayoutIncludesOpaqueBars
下面是Apple官方提供的文檔解釋,看過之后還是覺得太過于抽象济赎,于是用代碼來實(shí)驗(yàn)吧。
**edgesForExtendedLayout **
The extended edges to use for the layout.
**automaticallyAdjustsScrollViewInsets **
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
**extendedLayoutIncludesOpaqueBars **
A Boolean value indicating whether or not the extended layout includes opaque bars.
edgesForExtendedLayout
新建單個(gè)頁面的項(xiàng)目草添,然后加上UINavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
將edgesForExtendedLayout設(shè)置成UIRectEdgeNone育灸,表明View是不要擴(kuò)展到整個(gè)屏幕的腻窒。頁面效果如下:
typedef enum : NSUInteger {
UIRectEdgeNone = 0,
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight} UIRectEdge;
**automaticallyAdjustsScrollViewInsets **
這個(gè)屬性用于如果頁面是ScrollView或者UITableView儿子,通常我們希望ScrollView或者UITableView內(nèi)容顯示是在UINavigation Bar下面。通過設(shè)置edgesForExtendedLayout = UIRectEdgeNone或者self.navigationController.navigationBar.translucent =NO;可以讓view的布局從UINavigation Bar下面開始砸喻,不過一個(gè)副作用就是當(dāng)頁面滑動(dòng)的時(shí)候柔逼,view是沒有辦法占據(jù)全屏的。automaticallyAdjustsScrollViewInsets就可以很好的完成這個(gè)需求割岛。self.automaticallyAdjustsScrollViewInsets = NO;
這時(shí)UITableView會(huì)被UINavigation Bar遮擋住愉适。
self.automaticallyAdjustsScrollViewInsets = YES;
extendedLayoutIncludesOpaqueBars
如果狀態(tài)欄是不透明的,那么頁面的布局默認(rèn)是不會(huì)包含狀態(tài)欄的惠爽,除非將這個(gè)屬性設(shè)置成為YES癌蓖。所以如果你的頁面擴(kuò)展到Navigation Bar (edgesForExtendedLayout=UIRectEdgeAll),要是這個(gè)屬性設(shè)置成NO (default), 如果狀態(tài)欄是不透明的話婚肆,頁面是不會(huì)擴(kuò)展到狀態(tài)欄的租副。 在這篇文章http://redth.codes/ios7-full-screen-layout/ 里面提到有些時(shí)候automaticallyAdjustsScrollViewInsets并不能幫助我們正常計(jì)算ScrollView/TableView的Inset,這時(shí)候就自己設(shè)置咯旬痹。
self.myTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
UIView的sizeToFit:和fitSize方法文檔說明:
- (CGSize)sizeThatFits:(CGSize)size; // return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (void)sizeToFit; // calls sizeThatFits: with current view bounds and changes bounds size.
- (CGSize)sizeThatFits:(CGSize)size
讓View計(jì)算并返回subView最適合的大小 - sizeToFit
調(diào)整大小和移動(dòng)使其子視圖處于最佳位置
參考文章:
http://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjustsscrollviewinsets-extendedlayo
http://redth.codes/ios7-full-screen-layout/