美文网首页
tableView/scrollView默认内边距问题(cont

tableView/scrollView默认内边距问题(cont

作者: ImmortalSummer | 来源:发表于2019-08-28 15:26 被阅读0次

在scrollView/tableView等可互动控件中, 控制器隐藏导航栏后, 可滑动控件默认布局会有一个内边距, 顶部会把状态栏的高度空出来.

-(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];
}

以上代码是一个背景色为紫色的scrollView, 该scrollView添加了一个y=0的绿色的方块, 我们会看到如下图的效果:


存在默认内边距.png

如果要消除该默认内边距, 需要在viewDidLoad方法中添加:

if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

添加后效果如下:


去掉默认内边距.png

关于contentInsetAdjustmentBehavior属性

scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

可以参照:https://www.jianshu.com/p/b42030a37953

相关文章

网友评论

      本文标题:tableView/scrollView默认内边距问题(cont

      本文链接:https://www.haomeiwen.com/subject/oatmectx.html