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

出现问题时候的截图:

源码:

用到的类:

UIViewController+TitleTextAttributes.h 与 UIViewController+TitleTextAttributes.m

//
// UIViewController+TitleTextAttributes.h
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "NCTitleAttribute.h" @interface UIViewController (TitleTextAttributes) /**
* 设置当前控制器的标题属性
*
* @param attribute 属性对象
*/
- (void)titleTextAttributes:(NCTitleAttribute *)attribute; @end
//
// UIViewController+TitleTextAttributes.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "UIViewController+TitleTextAttributes.h" @implementation UIViewController (TitleTextAttributes) #pragma mark - public
- (void)titleTextAttributes:(NCTitleAttribute *)attribute
{
[self controller:self
titleTextAttributes:[attribute transformToDictionary]];
} #pragma mark - private
- (void)controller:(UIViewController *)controller titleTextAttributes:(NSDictionary *)dictionary
{
if ([controller isKindOfClass:[UIViewController class]]) {
[controller.navigationController.navigationBar setTitleTextAttributes:dictionary];
}
} @end

NCTitleAttribute.h 与 NCTitleAttribute.m

//
// NCTitleAttribute.h
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @interface NCTitleAttribute : NSObject @property (nonatomic, strong) UIColor *titleColor; // 标题颜色
@property (nonatomic, strong) UIFont *titleFont; // 标题字体 @property (nonatomic, strong) UIColor *shadowColor; // 阴影颜色
@property (nonatomic, assign) CGSize shadowOffset; // 阴影偏移量 // 将参数转换为字典
- (NSDictionary *)transformToDictionary; @end
//
// NCTitleAttribute.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "NCTitleAttribute.h" @implementation NCTitleAttribute - (NSDictionary *)transformToDictionary
{
NSMutableDictionary *dic = [NSMutableDictionary new]; if (_titleColor)
{
[dic setObject:_titleColor forKey:NSForegroundColorAttributeName];
}
else
{
[dic setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
} if (_titleFont)
{
[dic setObject:_titleFont forKey:NSFontAttributeName];
} if (_shadowOffset.height && _shadowOffset.width)
{
NSShadow *shadow = [NSShadow new]; shadow.shadowColor = _shadowColor;
shadow.shadowOffset = _shadowOffset; [dic setObject:shadow forKey:NSShadowAttributeName];
} return dic;
} @end

控制器源码:

//
// ViewController.m
// UIRectEdgeNone
//
// Created by YouXianMing on 14/10/29.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "UIViewController+TitleTextAttributes.h"
#import "NCTitleAttribute.h"
#import "WxHxD.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 初始化标题
[self initTitle]; // 背景view
UIView *backView = [[UIView alloc] initWithFrame:\
CGRectMake(, [WxHxD statusBarAndNavigationBarHeight],
[WxHxD screenWidth],
[WxHxD screenHeight] - [WxHxD statusBarAndNavigationBarHeight])];
backView.layer.borderWidth = .f;
backView.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:backView]; // tableView
_tableView = [[UITableView alloc] initWithFrame:backView.bounds
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[backView addSubview:_tableView]; } - (void)initTitle {
self.title = @"YouXianMing";
NCTitleAttribute *NCTitle = [NCTitleAttribute new];
NCTitle.titleColor = [UIColor redColor];
NCTitle.titleFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
[self titleTextAttributes:NCTitle];
} #pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reusedFlag = @"YouXianMing"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedFlag];
} cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
cell.textLabel.text = @"No Zuo No Die";
cell.textLabel.textColor = [UIColor grayColor]; return cell;
} @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. 个人总结(Alpha阶段)

    Alpha总结 我们在alpha 结束之后, 每位写一个博客, 回顾并总结自己的alpha 过程,哪些方面做的好的,哪些方面做得不足需要改进的 提出问题 同时,大家一定会在过程中产生了很多问题, 结合 ...

  2. C语言——<计算>_较大两个数相乘

    例题:9876543210*1234567890 的乘积 分析:正常的数据结构已经无法满足这么大的数相乘的结果.只能使用数组来进行操作. 1.两个数都用字符数组来接收. 2.接收后,因为每一位要乘以另 ...

  3. Node.js文件操作二

    前面的博客 Node.js文件操作一中主要是对文件的读写操作,其实还有文件这块还有一些其他操作. 一.验证文件path是否正确(系统是如下定义的) fs.exists = function(path, ...

  4. UIKit 框架之UITextView

    // // ViewController.m // UItextView // // Created by City--Online on 15/5/22. // Copyright (c) 2015 ...

  5. 有序列表ol和定义列表dl,dt,dd

    有序列表是一种讲究排序列表结构,使用<ol>标签定义,其中包含多个<li>列表项目.一般网页设计中,列表结构可以互用有序或者无序类表标签.但是,在强调项目排序栏目中,选用有序列 ...

  6. 撩课-Web大前端每天5道面试题-Day21

    1.对async.await的理解,内部原理? ①async---声明一个异步函数: 自动将常规函数转换成promise,返回值也是一个promise对象, 只有async函数内部的异步操作执行完,才 ...

  7. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  8. Hibernate中查询优化策略

    Hibernate查询优化策略 ² 使用延迟加载等方式避免加载多余数据 ² 通过使用连接查询,配置二级缓存.查询缓存等方式减少select语句数目 ² 结合缓存机制,使用iterate()方法减少查询 ...

  9. BZOJ4566: [Haoi2016]找相同字符(后缀自动机)

    题意 题目链接 Sol 直接在SAM上乱搞 枚举前缀,用SAM统计可以匹配的后缀,具体在匹配的时候维护和当前节点能匹配的最大值 然后再把parent树上的点的贡献也统计上,这部分可以爆跳parent树 ...

  10. 【代码笔记】iOS-archive保存图片到本地

    一,工程图: 二,代码: RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...