在scrollView/tableView等可互動(dòng)控件中, 控制器隱藏導(dǎo)航欄后, 可滑動(dòng)控件默認(rèn)布局會(huì)有一個(gè)內(nèi)邊距, 頂部會(huì)把狀態(tài)欄的高度空出來(lái).
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
scrollView.backgroundColor = [UIColor purpleColor];
scrollView.contentSize = CGSizeMake(0, [UIScreen mainScreen].bounds.size.height*2);
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
box.backgroundColor = [UIColor greenColor];
[scrollView addSubview:box];
}
以上代碼是一個(gè)背景色為紫色的scrollView, 該scrollView添加了一個(gè)y=0的綠色的方塊, 我們會(huì)看到如下圖的效果:

存在默認(rèn)內(nèi)邊距.png
如果要消除該默認(rèn)內(nèi)邊距, 需要在viewDidLoad方法中添加:
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
添加后效果如下:

去掉默認(rèn)內(nèi)邊距.png
關(guān)于contentInsetAdjustmentBehavior屬性
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;