tableView/scrollView默認(rèn)內(nèi)邊距問(wèn)題(contentInsetAdjustmentBehavior屬性)

在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;

可以參照:http://www.itdecent.cn/p/b42030a37953

?著作權(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)容

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