UITableView/UIScrollView内容视图下移

来源:互联网 发布:淘宝上怎么能卖酒 编辑:程序博客网 时间:2024/06/05 06:18

先来看看,普通控制上添加一个UITableView的情况:(设置tableView的背景颜色为蓝色)


再来看看实现的代码:

UITableView *tableView = [[UITableView allocinitWithFrame:CGRectMake(00self.view.bounds.size.width,self.view.bounds.size.height)];

    [self.view addSubview:tableView];

    self.tableView = tableView;

    

    self.tableView.backgroundColor = [UIColor blueColor];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

此时,设置的frame的y明明是0,但是,实际看到的tableViewframe确实是在0位置上(有背景蓝色),但是,它的contentInset的top方向上为64。内容视图向下移动了64,这是为什么呢?

经过百度和google之后,了解到,从ios7以后,控制器有了一个新的属性,automaticallyAdjustsScrollViewInsets, 默认是YES,如果视图里面存在唯一一个UIScorllView或其子类view,那么,它会自动设置相应的内边距,这样可以让scorll占据整个视图,又不会让导航栏遮盖。

如果,我们手动将automaticallyAdjustsScrollViewInsets设置为NO, 这时候看到的情况是:



此时代码:

self.automaticallyAdjustsScrollViewInsets = NO;

    

UITableView *tableView = [[UITableView allocinitWithFrame:CGRectMake(00self.view.bounds.size.width,self.view.bounds.size.height)];

    [self.view addSubview:tableView];

    self.tableView = tableView;

    

    self.tableView.backgroundColor = [UIColor blueColor];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

现在,它不会自动自动调整内边距了。


0 0
原创粉丝点击