UITableView顶部空白的N中解决办法

2018-10-17  本文已影响0人  OwenWong

一、iOS7在Controller中增加了automaticallyAdjustsScrollViewInsets这个属性,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖。但是这个属性在iOS11之后被遗弃了,新增了一个contentInsetAdjustmentBehavior属性:

if (@available(iOS 11.0, *)) {        

self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;    

}else {

self.automaticallyAdjustsScrollViewInsets = NO;

}

二、UINavigationBar的透明度设置

self.navigationController.navigationBar.translucent = NO;

当这个属性设为NO时,tableview会在上方留出64.f的高度给navigationbar

三、tableView  section的Header/Footer高度设置

如果这个Header/Footer的height设置为0时,系统会认为你没有设置,从而给一个默认40的高度;若不需要显示这两个view,将他们的height设置一个无限小的数字即可,常用:CGFLOAT_MIN。

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

    return CGFLOAT_MIN;

}

四、tableHeaderView、tableFooterView高度设置

以下操作会导致tableView顶部空白:

self.tableView.tableHeaderView = nil; 

self.tableView.tableHeaderView = [[UIView alloc] init]; 

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero]; 

同理,tableFooterView也是如此。footer和header只要设置了任意一个都会使两个地方都出现空白。

应这样设置:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];

 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];

上一篇下一篇

猜你喜欢

热点阅读