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):

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

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

可以看到:默認(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í)慣。