参考https://www.jb51.net/article/82465.htm

  1, self.automaticallyAdjustsScrollViewInsets = NO; 

        顶部的空白距离是64(20状态栏和44的navigationBar的距离)

  这个应该是最常见而且不容易被发现的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets这个属性,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖。

  PS:iOS7里面的布局问题挺多的,使用autolayout的时候会遇到好多,大概是因为iOS7新加入autolayout还还不成熟导致的吧。

  2,navigationbar设置问题

  虽然表面上看是tableview顶部有空白,但实际上可能是因为navigationbar设置问题导致。

   self.navigationController.navigationBar.translucent = NO; 这个属性设为no之后,tableview会在上方留出64.f的位置给navigationbar,也有小概率导致这个问题。

  3,tableview section header高度设置问题

顶部的空白距离是40,而且只有当tableView的style为UITableViewStyleGrouped才会出现;

  这个应该是新手遇到的比较多的。起因是iOS奇葩的逻辑,如果你设置header(或者footer)高度是0的话,系统会认为你没设置,然后将其设置为40.f。所以需要将其设置为一个较小的数:

?

1

2

3

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

return 0.001f;

}

  4,tableview的header、footer设置问题

  和3很像是不是?没发现区别吗?那就再读一次看看。这次是tableview的header视图引起的,而不是section的header高度引起。

对于tableview,不仅每个section有header,tableview整体也有header和footer,API如下:

?

1

2

@property (nonatomic, strong, nullable) UIView *tableHeaderView; // accessory view for above row content. default is nil. not to be confused with section header

@property (nonatomic, strong, nullable) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer

  这个header和footer要比section的header要和谐一些,只要你不去主动碰它就没事,但是如果你碰了...哼,哼...基本上会被设置出40.f高的间距。出现如下任意一行代码均会引起这个问题:

?

1

2

3

4

5

6

self.tableView.tableHeaderView = nil;

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

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

 self.tableView.tableFooterView = nil;

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

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

  对,你没想错,footerView也不能设置,footer和header只要设置了任意一个都会使两个地方都出现空白。不要问我为什么...

  当然,如果有的时候真的只需要其中一个view的话该怎么办呢?请如下设置:(似不似傻,自己建一个view呗,非得用着恶心的东西么...)  

?

1

2

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

  说白了,还是得设置成一个很小的高度,而不是0才行。

  关于tableView顶部空白的总结基本就这些了,如果想屏蔽的话,建议把这些写在baseTableViewController里面,这样就不用每次都扣这些东西了。宏懒得粘了,都是常见的,大家应该都能看懂。navigationbar那个,因为这个东西一般不在这里设置,写在base里面不是一个好的做法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

//

// HLNBaseTableViewController.m

// HLN-IMDemo

//

// Created by heiline on 15/8/25.

// Copyright (c) 2015年 baidu. All rights reserved.

//

#import "HLNBaseTableViewController.h"

@interface HLNBaseTableViewController ()

@end

@implementation HLNBaseTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.tableView = [[UITableView alloc] initWithFrame:(CGRect){CGPointZero, kScreenSize} style:_tableViewStyle];

[self.view addSubview:self.tableView];

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

if (self.navigationController != nil) {

self.tableView.height -= kNavBarH + kStatusBarH;

}

if (self.tabBarController != nil) {

if (self.navigationController.childViewControllers.count == 1) {

self.tableView.height -= kTabBarH;

}

}

self.tableView.delegate = self;

self.tableView.dataSource = self;

self.automaticallyAdjustsScrollViewInsets = NO;

}

- (void)dealloc {

self.tableView.dataSource = nil;

self.tableView.delegate = nil;

}

#pragma mark Table View Data Source And delegate Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 0;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 0;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

return [[UITableViewCell alloc] init];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

return nil;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

return nil;

}

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

return 0.001f;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 40.f;

}

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

return 0.001f;

}

@end

UITableView顶部多出一截空白问题相关推荐

  1. UITableView 顶部空白总结

    UITableView 顶部空白总结 开发中总存在适配问题,而tableView最容易出现顶部留白问题,我明明设置好的,为啥tableView偏移了,心中十万个CNM.不要担心,一般由以下问题导致. ...

  2. iOS开发Storyboard中UITableView顶部默认空白 - 芒果iOS

    [主要内容:] 1. 问题描述 2.问题分析 3. 解决问题办法 一.问题描述 前两天开发的时候在StoryBoard中创建了一个UITableView,但是拖到Controller里边之后,UITa ...

  3. iOS11隐藏导航条后顶部有20单位的空白或者pop页面时明显感觉前一个页面有上移的异常动画的解决办法。

    iOS11隐藏导航条后顶部有20单位的空白或者pop页面时明显感觉前一个页面有上移的异常动画的解决办法. 参考文章: (1)iOS11隐藏导航条后顶部有20单位的空白或者pop页面时明显感觉前一个页面 ...

  4. 为什么在iOS7中,UITableView顶部的UITableViewStyleGrouped样式具有额外的填充

    本文翻译自:Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in ...

  5. JimuReport积木报表打印多出一页空白页问题(解决方案)

    原文地址: JimuReport积木报表打印多出一页空白页问题 - BIGTREE (whwtree.com) 问题描述 积木报表预览或打印时,会多出一页空白页问题. 解决方案 总结如下: (1)方法 ...

  6. android 从顶部弹出的SnackBar

    在项目中需要用到从顶部弹出的SnackBar,于是在github找了一些代码看了下,并结合google 自家的SnackBar写了着么一个控件, github地址:https://github.com ...

  7. vant-Weapp实现省市区三级联动顶部弹出列表

    准备:利用vant weapp做的顶部弹出的省市区三级联动对话框 1.下载区域:arrea.js,放到utils备用 // 三级联动省市区 export default {province_list: ...

  8. iOS UIDocumentPickerViewController页面列表底部有一截空白【已解决】

    iOS UIDocumentPickerViewController 页面列表底部有一截空白? 求助大神,不知道是为什么?如图

  9. 高仿途牛App下拉顶部滑出更多

    现在旅游的App可谓已经很多了,携程,去哪,途牛.个人三个都用过,感觉途牛的体验还是比较不错的,个人体验,仅供参考. 好了,上面的一段扯淡衬托了我今天要和大家分享的一个功能效果,在途牛App的行程玩法 ...

最新文章

  1. App启动闪屏黑屏问题
  2. 数据挖掘之关联算法Apriori
  3. 【强化学习】DQN 的三种改进在运筹学中的应用
  4. 【遥感数字图像处理】实验:遥感图像分析方法大全(Erdas版)
  5. 每日一题:leetcode191.位1的个数
  6. 恭喜!已获8个院士的他,又新当选德国院士!
  7. 【Java基础篇】你真的了解构造器吗?
  8. 源码安装mysql_CentOS 7中源码安装MySQL 5.7.16 (亲测成功)
  9. Intel超线程技术 Hyper-Threading Technology (3) - 处理器资源与超线程(复制的资源)
  10. 何小鹏谈“小米造车”:我们要为勇敢者鼓掌
  11. android程序逆向工程
  12. PHP 下载远程图片
  13. ROBOTS.TXT在SEO优化中的运用(ROBOTS.TXT SEO优化实战)
  14. jsp如何编写java代码_如何在JSP页面内编写java代码?(代码示例)
  15. Buildroot用户手册
  16. java-数字转换汉语中人民币的大写
  17. unity接入 微信登录sdk
  18. 『牛角书』鸿蒙——简易通讯录项目开发
  19. 后台录屏、应用外录屏、跨应用录屏、直播屏幕、录屏扩展(ios)
  20. 自然语言处理中的Character Embedding技术

热门文章

  1. Android有用代码(二)
  2. android 实用代码
  3. python中合法的赋值语句y=y+1_关于Python赋值语句,以下选项中不合法的是 _________ 。_学小易找答案...
  4. 定时任务与网页去重、代理的使用
  5. crm系统技术(crm系统需要用到的技术)
  6. 如何做一个软件项目经理? ----写给公司所有的开发人员
  7. 揭秘开心农场开发团队:初期仅15万元创业基金
  8. oracle 表空间自动扩展及大小
  9. IoT赛道2019风向,升级为智联网AIoT,AI独角兽入场,“手机+AIoT”成标配【物女心经】
  10. 获取checkbox选中状态的两种方式_张童瑶的博客