iOS tableview上部白条在不同系统上的处理,

摘要:
1.出现白条的原因:iOS7viewcontroller自动添加新属性AdjustsScrollViewInsets,即是否自动调整insetself。automaticallyAdjustsScrollViewInsets=根据界面中导航栏和选项卡的高度,不显示滚动视图;2.以iOS11系统中的以下示例为例,向页面添加导航栏

1.白条产生的原因:

iOS 7 viewcontroller新增属性automaticallyAdjustsScrollViewInsets,即是否根据按所在界面的navigationbar与tabbar的高度,自动调整scrollview的 inset

self.automaticallyAdjustsScrollViewInsets = NO;

2.在iOS11系统以下 举个例子吧,对于有导航栏的页面添加一个tableview

- (void)setupTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

会自动留一个64高度的白条,用于导航栏

- (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的时候会好处理一点

3.iOS11 tableview的顶部设置

iOS11下是没有 automaticallyAdjustsScrollViewInsets 属性的,有一个替换属性 contentInsetAdjustmentBehavior

- (void)setupTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, UIScreenHeight-64) 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;
    }
}

4.iPhone X下的情况又有变化,顶部导航的高度不是64了,而是88,代码如下

#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 下又会有一些不同

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

iOS tableview上部白条在不同系统上的处理,第1张

发现第一组上部与导航有一个距离,不合理

补充:对于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;

}

iOS tableview上部白条在不同系统上的处理,第2张

iOS tableview上部白条在不同系统上的处理,第3张

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

免责声明:文章转载自《iOS tableview上部白条在不同系统上的处理,》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS项目崩溃日志采集与分析第一章--django--安装介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

读取配置文件.ini

思路:先创建一个配置文件(如: config.ini ),然后再新建一个.py文件读取; 1、创建.ini 配置文件,(后期把运用到的配置文件都可以放到这个里面,统一管理) [DATABASE] host = XX.XX.X.Xusername = rootpasswod = 123456port = 24306database = gv-crcs[H...

iOS-截取TableView生成图片

先看一下实例效果: 如果所示,这是一个在APP中截图,并调起微信客户端,发送给好友的例子,图片就是一个tableView的截图。 先实现一个小例子,如果tableVIew里面的内容,没有超过当前屏幕显示的区域,我们可以直接根据tableView的frame,生成一张图片 // 根据view生成图片 UIGraphicsBeginImageCo...

Jmeter-Critical Section Controller(临界区控制器)(还没看,是一个控制请求按顺序执行的东东)

The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will be executed by only one thread as a named lock will be taken before executing...

iOS中上拉加载下拉刷新之MJRefresh

1.导入到自己的工程中,如果是非ACR,则需要-fno-objc-arc,如果是arc就不用管了。 一般的刷新都是基于UIScrollView的,因为能拖拽的view一般都是继承于UIScrollView。 2.#import “MJRefresh.h”   这个可以直接加载.m中 然后.h文件中: 1 #import <UIKit/UIKit.h&...

C#读写ini文件详解

  原文地址:http://developer.51cto.com/art/200908/143715.htm C#读写ini文件之前要了解的概念:INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打开,主要存放的是用户所做的选择或系统的各种参数. C#读写ini文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION...

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

一、实现效果 二、使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性只需要设置一次,比如字...