解决UITableView在iOS7中UINavigationController里的顶部留白问题

出现问题时候的截图:

源码:

用到的类:

UIViewController+TitleTextAttributes.h 与 UIViewController+TitleTextAttributes.m

  1. //
  2. // UIViewController+TitleTextAttributes.h
  3. // YouXianMing
  4. //
  5. // Created by YouXianMing on 14-9-20.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import "NCTitleAttribute.h"
  11.  
  12. @interface UIViewController (TitleTextAttributes)
  13.  
  14. /**
  15. * 设置当前控制器的标题属性
  16. *
  17. * @param attribute 属性对象
  18. */
  19. - (void)titleTextAttributes:(NCTitleAttribute *)attribute;
  20.  
  21. @end
  1. //
  2. // UIViewController+TitleTextAttributes.m
  3. // YouXianMing
  4. //
  5. // Created by YouXianMing on 14-9-20.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "UIViewController+TitleTextAttributes.h"
  10.  
  11. @implementation UIViewController (TitleTextAttributes)
  12.  
  13. #pragma mark - public
  14. - (void)titleTextAttributes:(NCTitleAttribute *)attribute
  15. {
  16. [self controller:self
  17. titleTextAttributes:[attribute transformToDictionary]];
  18. }
  19.  
  20. #pragma mark - private
  21. - (void)controller:(UIViewController *)controller titleTextAttributes:(NSDictionary *)dictionary
  22. {
  23. if ([controller isKindOfClass:[UIViewController class]]) {
  24. [controller.navigationController.navigationBar setTitleTextAttributes:dictionary];
  25. }
  26. }
  27.  
  28. @end

NCTitleAttribute.h 与 NCTitleAttribute.m

  1. //
  2. // NCTitleAttribute.h
  3. // YouXianMing
  4. //
  5. // Created by YouXianMing on 14-9-20.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface NCTitleAttribute : NSObject
  12.  
  13. @property (nonatomic, strong) UIColor *titleColor; // 标题颜色
  14. @property (nonatomic, strong) UIFont *titleFont; // 标题字体
  15.  
  16. @property (nonatomic, strong) UIColor *shadowColor; // 阴影颜色
  17. @property (nonatomic, assign) CGSize shadowOffset; // 阴影偏移量
  18.  
  19. // 将参数转换为字典
  20. - (NSDictionary *)transformToDictionary;
  21.  
  22. @end
  1. //
  2. // NCTitleAttribute.m
  3. // YouXianMing
  4. //
  5. // Created by YouXianMing on 14-9-20.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "NCTitleAttribute.h"
  10.  
  11. @implementation NCTitleAttribute
  12.  
  13. - (NSDictionary *)transformToDictionary
  14. {
  15. NSMutableDictionary *dic = [NSMutableDictionary new];
  16.  
  17. if (_titleColor)
  18. {
  19. [dic setObject:_titleColor forKey:NSForegroundColorAttributeName];
  20. }
  21. else
  22. {
  23. [dic setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
  24. }
  25.  
  26. if (_titleFont)
  27. {
  28. [dic setObject:_titleFont forKey:NSFontAttributeName];
  29. }
  30.  
  31. if (_shadowOffset.height && _shadowOffset.width)
  32. {
  33. NSShadow *shadow = [NSShadow new];
  34.  
  35. shadow.shadowColor = _shadowColor;
  36. shadow.shadowOffset = _shadowOffset;
  37.  
  38. [dic setObject:shadow forKey:NSShadowAttributeName];
  39. }
  40.  
  41. return dic;
  42. }
  43.  
  44. @end

控制器源码:

  1. //
  2. // ViewController.m
  3. // UIRectEdgeNone
  4. //
  5. // Created by YouXianMing on 14/10/29.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "UIViewController+TitleTextAttributes.h"
  11. #import "NCTitleAttribute.h"
  12. #import "WxHxD.h"
  13.  
  14. @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
  15. @property (nonatomic, strong) UITableView *tableView;
  16. @end
  17.  
  18. @implementation ViewController
  19.  
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22.  
  23. // 初始化标题
  24. [self initTitle];
  25.  
  26. // 背景view
  27. UIView *backView = [[UIView alloc] initWithFrame:\
  28. CGRectMake(, [WxHxD statusBarAndNavigationBarHeight],
  29. [WxHxD screenWidth],
  30. [WxHxD screenHeight] - [WxHxD statusBarAndNavigationBarHeight])];
  31. backView.layer.borderWidth = .f;
  32. backView.layer.borderColor = [UIColor redColor].CGColor;
  33. [self.view addSubview:backView];
  34.  
  35. // tableView
  36. _tableView = [[UITableView alloc] initWithFrame:backView.bounds
  37. style:UITableViewStylePlain];
  38. _tableView.delegate = self;
  39. _tableView.dataSource = self;
  40. [backView addSubview:_tableView];
  41.  
  42. }
  43.  
  44. - (void)initTitle {
  45. self.title = @"YouXianMing";
  46. NCTitleAttribute *NCTitle = [NCTitleAttribute new];
  47. NCTitle.titleColor = [UIColor redColor];
  48. NCTitle.titleFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
  49. [self titleTextAttributes:NCTitle];
  50. }
  51.  
  52. #pragma mark - 代理
  53. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  54. return ;
  55. }
  56.  
  57. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  58. static NSString *reusedFlag = @"YouXianMing";
  59.  
  60. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];
  61. if (cell == nil) {
  62. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  63. reuseIdentifier:reusedFlag];
  64. }
  65.  
  66. cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
  67. cell.textLabel.text = @"No Zuo No Die";
  68. cell.textLabel.textColor = [UIColor grayColor];
  69.  
  70. return cell;
  71. }
  72.  
  73. @end

如何解决呢?很简单:

添加以下代码:

// 让边缘留白为空

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

if (systemVersion >= 7.0) {

self.edgesForExtendedLayout = UIRectEdgeNone;

}

效果:

注意:此种问题只有在iOS7以上才会出现

解决UITableView在iOS7中UINavigationController里的顶部留白问题的更多相关文章

  1. UITableView中cell里的UITextField不被弹出键盘挡住

    UITableView中cell里的UITextField不被弹出键盘挡住 本人视频教程系类   iOS中CALayer的使用 效果如下: 源码: EditCell.h 与 EditCell.m // ...

  2. WWDC 2013 Session笔记 - iOS7中的多任务

    这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或使用,但是还烦请保留原文链接,谢谢您的理解合作.如果您觉得本站对您能有帮助, ...

  3. ios7中的多任务

    转自:http://onevcat.com/2013/08/ios7-background-multitask/ WWDC 2013 Session笔记 - iOS7中的多任务 iOS7的后台多任务特 ...

  4. [ios-必看] WWDC 2013 Session笔记 - iOS7中的多任务【转】

    感谢:http://onevcat.com/2013/08/ios7-background-multitask/ http://www.objc.io/issue-5/multitasking.htm ...

  5. UITableView使用过程中可能遇到的问题

    前言:记录一些UITableView使用过程中可能遇到的问题 环境:Xcode9 解决UITableViewStyleGrouped类型的TableView的cell距离顶部有距离的问题: table ...

  6. iOS7中的多任务 - Background Fetch,Silent Remote Notifications,Background Transfer Service

    转自:http://onevcat.com/2013/08/ios7-background-multitask/ 在IOS 7 出来不就,公司内部也组织了一次关于IOS 7 特性的的分享,今天看见on ...

  7. iOS7中的多任务I

    [改变了后台任务的运行方式] 在iOS6和之前的系统中,系统在用户退出应用后,如果应用正在执行后台任务的话,系统会保持活跃状态直到后台任务完成或者是超时以后,才会进入真正的低功耗休眠状态. 而在iOS ...

  8. iOS7中的ViewController切换

    转自:https://onevcat.com/2013/10/vc-transition-in-ios7/ iOS 7 SDK之前的VC切换解决方案 在深入iOS 7的VC切换效果的新API实现之前, ...

  9. iOS7中计算UILabel中字符串的高度

    iOS7中计算UILabel中字符串的高度 iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UIL ...

随机推荐

  1. JavaScript引用类型和值类型

    thead>tr>th,.table>tbody>tr>th,.table>tfoot>tr>th,.table>thead>tr>t ...

  2. 008.在C#中,显式接口VS隐式接口

    原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...

  3. SQL——Sql_Server中如何判断表中某字段、判断表、判断存储过程以及判断函数是否存在

    一.比如说要判断表A中的字段C是否存在两个方法: (1) 直接查表——有点笨,有点常规 IF EXISTS ( SELECT 1 FROM SYSOBJECTS T1 INNER JOIN SYSCO ...

  4. java自学-运算符

    上一篇介绍了java的变量和数据类型,要对数据处理,还需要用到java运算符,这里只总结下常用的,运算符主要分为以下几类:1 算数运算符 算数运算符包括: + 加法运算 - 减法运算 * 乘法运算 / ...

  5. Telnet 模拟邮件发送过程

    Telnet 模拟邮件发送过程 windows要提前开启Telnet客户端的功能,再按照下面步骤完成邮件发送: 1.通过 cmd 进入命令窗口 2.连接要发送邮件的服务器:telnet smtp.al ...

  6. 悟空模式-java设计模式

    目前已定义的java设计模式细分下来有二十余种,这篇博客主要是想从大家所熟知的孙悟空入手,阐述各个设计模式的概念和优缺点,以及他们之间的联系. 在下面介绍的每个设计模式里,都会有与西游记相关的具体案例 ...

  7. ZooKeeper 节点

    ZNode zk 中的每一个数据节点称为 ZNode ,所有的 ZNode 按层次化结构进行组织,形成一棵树(与Linux文件系统相似).节点可以写入数据,也可以在节点下面创建子节点. 节点类型: 1 ...

  8. 虚拟机VMware workstations的网络设置

    一般遇到虚拟机中上不了网的问题,可以这样解决: 1.在终端输入命令:ifconfig.--查看eth0接口上是否有IP地址. 发现eth0接口上没有ip地址. 2.输入cat /etc/sysconf ...

  9. time模块,计算时间差

    计算当前时间与所输入的时间的时间差 #1 计算当前时间的时间戳时间 t_now = time.time() # 计算以前的时间的时间戳时间 t_before = input('请输入时间(例如:200 ...

  10. JS 自定义对象 属性

    js自定义对象 一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在J ...