1.白條產生的緣由:佈局
iOS 7 viewcontroller新增屬性automaticallyAdjustsScrollViewInsets,便是否根據按所在界面的navigationbar與tabbar的高度,自動調整scrollview的 insetui
self.automaticallyAdjustsScrollViewInsets = NO;
2.在iOS11系統如下 舉個例子吧,對於有導航欄的頁面添加一個tableviewspa
- (void)setupTableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight) style:(UITableViewStylePlain)]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; }
會自動留一個64高度的白條,用於導航欄3d
- (void)setupTableView { self.automaticallyAdjustsScrollViewInsets = NO; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, UIScreenHeight-64) style:(UITableViewStylePlain)]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; }
效果是同樣的,可是推薦後面這種佈局,由於要適配iOS11和iPhone X的時候會好處理一點code
3.iOS11 tableview的頂部設置orm
iOS11下是沒有 automaticallyAdjustsScrollViewInsets 屬性的,有一個替換屬性 contentInsetAdjustmentBehaviorblog
- (void)setupTableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, UIScreenHeight-64) style:(UITableViewStylePlain)]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self;
}
4.iPhone X下的狀況又有變化,頂部導航的高度不是64了,而是88,代碼以下string
#define UIScreenWidth ([UIScreen mainScreen].bounds.size.width) #define UIScreenHeight ([UIScreen mainScreen].bounds.size.height) #define IphoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO) - (void)setupTableView { CGFloat top = 64; if (IphoneX) { top += 24; } _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, top, UIScreenWidth, UIScreenHeight-top) style:(UITableViewStylePlain)]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } }
5.以上都是tableview的類型爲 UITableViewStylePlain的狀況下,在分組類型 UITableViewStyleGrouped 下又會有一些不一樣it
- (void)setupData { NSString *strName; self.dataSource = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i++) { strName = [NSString stringWithFormat:@"第%d行",i]; [self.dataSource addObject:strName]; } } - (void)setupTableView { CGFloat top = 64; if (IphoneX) { top += 24; } _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, top, UIScreenWidth, UIScreenHeight-top) style:(UITableViewStyleGrouped)]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } } #pragma mark -- UITableViewDelegate, UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return 1; } else if(section == 1) { return 3; } else { return 6; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *flag=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag]; } if (indexPath.section == 0) { [cell.textLabel setText:self.dataSource[indexPath.row]]; } else if (indexPath.section == 1) { [cell.textLabel setText:self.dataSource[indexPath.row+1]]; } else { [cell.textLabel setText:self.dataSource[indexPath.row+4]]; } return cell; }
發現第一組上部與導航有一個距離,不合理io
補充:對於UITableView的group類型,若是設置heightForHeaderInSection,那麼也必需要設置heightForFooterInSection,不然的話heightForFooterInSection會被系統默認設置爲15.0f
注意:其實分組的空隙就是headview的高度,分組的顏色就是tableview的背景色
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section != 0) {
return 20;
} else {
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* myView = [[UIView alloc] init];
myView.backgroundColor = [UIColor greenColor];
return myView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView* myView = [[UIView alloc] init];
return myView;
}
6.其餘tableview的一些設置狀況
//tableview分界線與tableview左邊邊界相距13px [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 13, 0, 0)]; //tableview最後一條cell能夠划動的最大距離tableview底部的距離爲90 self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0); //tableview沒有更多數據的時候不顯示多餘的空白cell self.tableView.tableFooterView = [UIView new]; //cell右邊顯示的一些玩意 //灰色小箭頭 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //藍色感嘆號加灰色小箭頭 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; //藍色對勾 cell.accessoryType = UITableViewCellAccessoryCheckmark; //藍色感嘆號 cell.accessoryType = UITableViewCellAccessoryDetailButton; //解決iOS11tableview刷新數據的時候頁面會抖動 self.tableView.estimatedRowHeight = 0; self.tableView.estimatedSectionFooterHeight = 0; self.tableView.estimatedSectionHeaderHeight = 0; //tableview添加xib cell 和 手動建立cell 的兩種方式 //xib 佈局cell [self.tableView registerNib:[UINib nibWithNibName:@"FNGroupFileMenuTableViewCell" bundle:nil] forCellReuseIdentifier:@"groupFileMenuCellIdentifier"]; //手動建立cell [self.tableView registerClass:[FNAgendaListTableViewCell class] forCellReuseIdentifier:kFNAgendaListCellIdentifier];