全屏布局(fullScreenLayout)那些事

圖片來(lái)源@百度圖片

當(dāng)我們使用UINavigationController時(shí),插入一個(gè)控制器,然后往這個(gè)控制器的view上加subview(比如一個(gè)tableView)時(shí),經(jīng)常會(huì)碰到tableView的實(shí)際展示跟自己設(shè)置的frame不一致的情況。這里就總結(jié)記錄一下平時(shí)自己遇到過(guò)的相關(guān)問(wèn)題。

iOS7之前控制器有一個(gè)屬性wantsFullScreenLayout。當(dāng)把它設(shè)置為YES時(shí),你添加的subview的y就是從屏幕的最頂部開(kāi)始算,包含navigationbar,設(shè)置為NO時(shí),則從navigationbar的bottom開(kāi)始算。因?yàn)閕OS7之前,navigationbar默認(rèn)一般是不透明的,所以wantsFullScreenLayout一般默認(rèn)為NO。從iOS7開(kāi)始,這個(gè)屬性被廢棄了,代替它的是三個(gè)新的屬性:edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets,今天重點(diǎn)說(shuō)的就是這三個(gè)經(jīng)常用的屬性。

edgesForExtendedLayout

The extended edges to use for the layout.
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

這個(gè)屬性是通過(guò)擴(kuò)展子視圖的邊緣來(lái)適配屏幕,因?yàn)閕OS7之后鼓勵(lì)全屏,navigationbar變得半透明,所以這個(gè)屬性默認(rèn)為UIRectEdgeAll,及x和y是從屏幕的左上角開(kāi)始算的。比如設(shè)置tableView的frame如下:

self.tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 350);

這里為了防止automaticallyAdjustsScrollViewInsets的干擾,我們?cè)?code>viewDidAppear:方法里加上一句代碼,下文會(huì)細(xì)說(shuō)。

// 相當(dāng)于把a(bǔ)utomaticallyAdjustsScrollViewInsets設(shè)置為NO了
self.tableView.contentInset = UIEdgeInsetsZero;

效果如下圖:

UIRectEdgeAll

當(dāng)如下更改edgesForExtendedLayout的設(shè)置時(shí)

self.edgesForExtendedLayout = UIRectEdgeNone;

效果如下:

UIRectEdgeNone

顯然這是我們要的tableView效果,但是我們發(fā)現(xiàn)navigationbar的顏色變灰了,這是因?yàn)閚avigationbar是半透明的,但是navigationbar下面沒(méi)有視圖。

這時(shí)我們可以通過(guò)改變navigationbar的透明度來(lái)實(shí)現(xiàn)。

self.navigationController.navigationBar.translucent = NO;   
//    self.edgesForExtendedLayout = UIRectEdgeNone;

設(shè)置不透明之后,subview的邊緣就擴(kuò)展不到頂部上了,只能到navigationbar的底部。效果

extendedLayoutIncludesOpaqueBars

A Boolean value indicating whether or not the extended layout includes opaque bars.
The default value of this property is NO

這個(gè)屬性是指當(dāng)navigationbar不透明時(shí),layout是否包含navigationbar。當(dāng)navigationbar透明時(shí),此屬性不起作用。比如在上面設(shè)置不透明的基礎(chǔ)上,把這個(gè)屬性設(shè)置為YES(該屬性默認(rèn)NO),

self.navigationController.navigationBar.translucent = NO;  
//    self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;

發(fā)現(xiàn)tableView的y從屏幕最頂部開(kāi)始算了。

extendedLayoutIncludesOpaqueBars

automaticallyAdjustsScrollViewInsets

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
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.

這個(gè)屬性太強(qiáng)大了,它會(huì)根據(jù)navigationbar和tabbar的存在,自動(dòng)調(diào)整scrollView的contentInset。如下圖

automaticallyAdjustsScrollViewInsets

透過(guò)半透明的navigationbar可以看到tableView的y其實(shí)還是在屏幕的最頂部,但是第一個(gè)cell已經(jīng)變成從navigationbar的底部開(kāi)始了,這是因?yàn)?code>automaticallyAdjustsScrollViewInsets屬性默認(rèn)為YES,tableView的contentInset已經(jīng)由UIEdgeInsetsZero自動(dòng)調(diào)整為UIEdgeInsetsMake(64, 0, 0, 0)了。當(dāng)我們?cè)O(shè)置self.automaticallyAdjustsScrollViewInsets = NO;時(shí),就變成上文第一張效果圖的效果了。但是它也有不足,就是scrollview必須是根視圖添加的第一個(gè)子視圖,否則此屬性無(wú)效。

最后

這都是作者在實(shí)際工作中的一些總結(jié),如有不對(duì)之處,歡迎指正。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • /* UIViewController is a generic controller base class th...
    DanDanC閱讀 2,054評(píng)論 0 2
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,981評(píng)論 1 9
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,738評(píng)論 1 14
  • 嗨,你們好 嗨,好久不見(jiàn) 叮咚,門(mén)鈴響了 一如既往的打開(kāi)一扇門(mén),二扇門(mén) 突然來(lái)了一句:“小王,今天讓你做的,做完了...
    _似錦年華閱讀 550評(píng)論 0 2
  • 無(wú)意遇到簡(jiǎn)書(shū),看著下載評(píng)論里很多人因簡(jiǎn)書(shū)而興奮和滿足,被他們感染了,也下載了下來(lái)。對(duì)于我這種接受填鴨式教育且...
    f29e1084117a閱讀 204評(píng)論 0 0

友情鏈接更多精彩內(nèi)容