UIViewController系列之extendedLayout,uiviewcontroller

來源:互聯網
上載者:User

UIViewController系列之extendedLayout,uiviewcontroller

參考文章:

http://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjustsscrollviewinsets-extendedlayo

http://redth.codes/ios7-full-screen-layout/

 

iOS 7以後在ViewController裡面引進了一系列屬性用於管理頁面配置。

 

下面是Apple官方提供的文檔解釋,看過之後還是覺得太過於抽象,於是用代碼來實驗吧。

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 建立單個頁面的項目,然後加上UINavigationController 把背景設定成紅色,介面效果如下: 所以可以看到在iOS7後,View的布局是預設全屏的,Navigation Bar預設是半透明的,於是在Navigation Bar下面可以看到紅色的背景。  
- (void)viewDidLoad {    [super viewDidLoad];    self.edgesForExtendedLayout = UIRectEdgeNone;}

將edgesForExtendedLayout設定成UIRectEdgeNone,表明View是不要擴充到整個螢幕的。頁面效果如下:

 

UIRectEdge是個枚舉類型,其他的值通過字面意思也是非常容易理解的。

typedef enum : NSUInteger {   UIRectEdgeNone   = 0,   UIRectEdgeTop    = 1 << 0,   UIRectEdgeLeft   = 1 << 1,   UIRectEdgeBottom = 1 << 2,   UIRectEdgeRight  = 1 << 3,   UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight } UIRectEdge;

 

automaticallyAdjustsScrollViewInsets 

這個屬性用於如果頁面是ScrollView或者UITableView,通常我們希望ScrollView或者UITableView內容顯示是在UINavigation Bar下面。

通過設定edgesForExtendedLayout = UIRectEdgeNone或者self.navigationController.navigationBar.translucent = NO;可以讓view的布局從UINavigation Bar下面開始,不過一個副作用就是當頁面滑動的時候,view是沒有辦法佔據全屏的。

automaticallyAdjustsScrollViewInsets就可以很好的完成這個需求。

self.automaticallyAdjustsScrollViewInsets = NO;

這時UITableView會被UINavigation Bar遮擋住。

 

self.automaticallyAdjustsScrollViewInsets = YES;

這時可以看到UITableView的內容會從UINavigation Bar下面開始,並且這個頁面的View還是佔據整個螢幕的,所以這一個屬性完全搞定!

 

extendedLayoutIncludesOpaqueBars

如果狀態列是不透明的,那麼頁面的布局預設是不會包含狀態列的,除非將這個屬性設定成為YES。所以如果你的頁面擴充到Navigation Bar (edgesForExtendedLayout=UIRectEdgeAll),要是這個屬性設定成NO (default), 如果狀態列是不透明的話,頁面是不會擴充到狀態列的。 

 

在這篇文章http://redth.codes/ios7-full-screen-layout/裡面提到有些時候automaticallyAdjustsScrollViewInsets並不能協助我們正常計算ScrollView/TableView的Inset,這時候就自己設定咯。

self.myTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.