iOS中导航栏与UITableView布局冲突问题

情况描述:

我们需要实现下图效果

页面一.png

分析过程

1、此页面还有其他几个页面都非常相似,考虑到这种情况,想到两种解决方法:
(1)使用UITextField等View控件解决
(2)使用UITableView,再自定义cell解决(UIViewController具有导航栏,需要在导航栏下添加一个tableView)
本文是使用第二种方法发生的Bug解决方案

self.automaticallyAdjustsScrollViewInsets = NO;解决navigation的一个自适应scrollView产生的一个高度问题

图层.png

首先,要知道的是UITableView继承于UIScrollView
详情请查看以下链接
http://www.tuicool.com/articles/ymi22e
http://www.th7.cn/Program/IOS/201409/274079.shtml

代码一:UITableViewStylePlain下

#define SWIDTH [UIScreen mainScreen].bounds.size.width
@interface loginViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

@end
- (void)viewDidLoad {
    [super viewDidLoad];

如果没有这句代码会发生下图情况:cell与TableView具有高度差
self.automaticallyAdjustsScrollViewInsets = NO;

Plain下Bug情形.png
    [self  createTableView];
}
#pragma mark------自定义方法
- (void)createTableView{

    self.tableView = [[UITableView alloc]   initWithFrame:CGRectMake(0,64, SWIDTH, 200) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor purpleColor];
    self.tableView.rowHeight = 50;
    self.tableView.scrollEnabled = NO;

    [self.view addSubview:self.tableView];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
}

#pragma mark------ UITableViewDataSource,UITableViewDelegate方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.textLabel.text = @"你好";
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

代码二:UITableViewStylePlain下

#define SWIDTH [UIScreen mainScreen].bounds.size.width
@interface loginViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

@end
- (void)viewDidLoad {
    [super viewDidLoad];

如果没有这句代码会发生下图情况:cell与TableView具有高度差
self.automaticallyAdjustsScrollViewInsets = NO;

Group下BUG情形.png

Group下BUG解决后.png
    [self  createTableView];
}
#pragma mark------自定义方法
- (void)createTableView{

    self.tableView = [[UITableView alloc]   initWithFrame:CGRectMake(0,64, SWIDTH, 200) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor purpleColor];
    self.tableView.rowHeight = 50;
    self.tableView.scrollEnabled = NO;

    [self.view addSubview:self.tableView];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
}

#pragma mark------ UITableViewDataSource,UITableViewDelegate方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.textLabel.text = @"你好";
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

/*
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 10)];
     v.backgroundColor = [UIColor redColor];
    return v;
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

Group下注意事项:

1、分区头和分区尾的高度设置不能为0,如果设为0,是没有效果的。但是我们可以设置成一个很小的数值。
2、分区头尾的设置有属性和方法两种设置方式,如果使用方法设置viewForHeaderInSection:和heightForHeaderInSection:这两个方法中的高度要保持一致。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,295评论 3 38
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,989评论 2 10
  • 问题有摘自http://ayjkdev.top/2016/03/18/solve-questions/ 问题排序 ...
    乱尘阅读 2,109评论 0 0
  • UITableView 表格视图一 UITableView1.1是什么?以列表的方式展示数据的一种控件,且继承自...
    037e3257fa3b阅读 331评论 0 1
  • 已经是几年前的故事了。 小镇只有两个季节,夏天和冬天。而在那些夏天和冬天里,我总是陪着斑斑玩一种叫做文字拓展的游戏...
    歪猪阅读 299评论 16 19

友情链接更多精彩内容