edgesForExtendedLayout與automaticallyAdjustsScrollViewInsets

edgesForExtendedLayout:
在IOS7以后 ViewController 開(kāi)始使用全屏布局,而且是默認(rèn)的行為通常涉及到布局,就離不開(kāi)這個(gè)屬性edgesForExtendedLayout,它是一個(gè)類(lèi)型為UIExtendedEdge的屬性,指定邊緣要延伸的方向,它的默認(rèn)值很自然的是UIRectEdgeAll,四周邊緣均延伸,就是說(shuō),如果視圖中上有navigationBar,下有tabBar,那么視圖仍會(huì)延伸覆蓋到四周的區(qū)域(坐標(biāo)原點(diǎn)在屏幕左上角)。因?yàn)橐话銥榱瞬蛔宼ableView 不延伸到 navigationBar 下面, 屬性設(shè)置為 UIRectEdgeNone,這樣坐標(biāo)原點(diǎn)在導(dǎo)航欄下面。
automaticallyAdjustsScrollViewInsets:
系統(tǒng)會(huì)根據(jù)navigationBar和tabBar的位置,自動(dòng)調(diào)整UIScrollView的內(nèi)容位置,默認(rèn)值是YES。如果想自己調(diào)整,設(shè)為NO。
上代碼咱們跑一下:
這是程序的根視圖控制器:

#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//self.edgesForExtendedLayout = UIRectEdgeNone;
//self.automaticallyAdjustsScrollViewInsets = NO;
//self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor= [UIColorgrayColor];
UITableView*tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height-64)style:UITableViewStylePlain];
tableView.backgroundColor= [UIColorredColor];
tableView.delegate=self;
tableView.dataSource=self;
[self.viewaddSubview:tableView];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return50;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return30;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*cellId =@"CellId";
UITableViewCell*cell = [tableViewcellForRowAtIndexPath:indexPath];
if(!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];
}
cell.textLabel.text= [NSStringstringWithFormat:@"%ld",indexPath.row];
returncell;
}

一、默認(rèn)跑起來(lái):

edgesForExtendedLayout 與automaticallyAdjustsScrollViewInsets均是默認(rèn)值


可以發(fā)現(xiàn)默認(rèn)原點(diǎn)在屏幕左上角,tableView的內(nèi)容視圖向下偏移了64個(gè)像素。
二、將edgesForExtendedLayout 置為 UIRectEdgeNone:

edgesForExtendedLayout 置為 UIRectEdgeNone


可以看到原點(diǎn)坐標(biāo)變成了在導(dǎo)航條下面。
這時(shí)候?qū)utomaticallyAdjustsScrollViewInsets設(shè)為NO已經(jīng)沒(méi)有任何意義,因?yàn)楝F(xiàn)在tableView的內(nèi)容視圖的位置完全正確。
三、或者 edgesForExtendedLayout設(shè)為默認(rèn), self.navigationController.navigationBar.translucent=NO:

self.navigationController.navigationBar.translucent=NO


可以看到效果與edgesForExtendedLayout 置為 UIRectEdgeNone一樣。
四、如果將automaticallyAdjustsScrollViewInsets設(shè)置為NO,將edgesForExtendedLayout設(shè)為默認(rèn),我們來(lái)看看:

automaticallyAdjustsScrollViewInsets為NO,edgesForExtendedLayout為默認(rèn)


可以看到:默認(rèn)原點(diǎn)在屏幕左上角,tableView的內(nèi)容視圖也在左上角。
總結(jié):
1.edgesForExtendedLayout默認(rèn)向四周延展,坐標(biāo)原點(diǎn)在屏幕左上角。設(shè)置為None可以將原點(diǎn)設(shè)置在導(dǎo)航條下面。
2.automaticallyAdjustsScrollViewInsets:系統(tǒng)自動(dòng)適配scrollView的內(nèi)容視圖位置,設(shè)為NO,自己去適配
3.self.navigationController.navigationBar.translucent=NO與edgesForExtendedLayout = None的效果一樣。后者會(huì)使得導(dǎo)航條變灰,如需改變,仍然需要前者的配置,所以可以直接用前者來(lái)將原點(diǎn)下調(diào)到導(dǎo)航條下面。
4.如何設(shè)置這兩個(gè)屬性,需要參照項(xiàng)目的UI圖,一般將原點(diǎn)調(diào)在導(dǎo)航條下面,這樣符合ios7之前的編程習(xí)慣。

最后編輯于
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,751評(píng)論 1 14
  • 先聲明:以下總結(jié)只對(duì)ios7及ios7之后才有效~~~ 之前開(kāi)發(fā)過(guò)程中偶爾會(huì)遇到設(shè)置導(dǎo)航欄透明與否或者運(yùn)行系統(tǒng)版本...
    Qiu_W閱讀 1,528評(píng)論 0 1
  • 總所周知,蘋(píng)果從iOS7開(kāi)始采用扁平化的界面風(fēng)格,顛覆了果粉們“迷戀”的擬物化風(fēng)格。對(duì)于開(kāi)發(fā)者而言,全新的風(fēng)格帶來(lái)...
    聰明的笨白閱讀 3,625評(píng)論 8 40
  • 廢話(huà)不多說(shuō),直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,687評(píng)論 0 15
  • 六月風(fēng)花,本該是世間很美的風(fēng)景,木槿花嬌羞的綻放著,漫天空的蒲公英隨風(fēng)起舞……然而在二十年后的他的眼里,這一切...
    博昊淇晟閱讀 317評(píng)論 0 0

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