解决UITableView在iOS7中UINavigationController里的顶部留白问题
解决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里的顶部留白问题的更多相关文章
- UITableView中cell里的UITextField不被弹出键盘挡住
UITableView中cell里的UITextField不被弹出键盘挡住 本人视频教程系类 iOS中CALayer的使用 效果如下: 源码: EditCell.h 与 EditCell.m // ...
- WWDC 2013 Session笔记 - iOS7中的多任务
这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或使用,但是还烦请保留原文链接,谢谢您的理解合作.如果您觉得本站对您能有帮助, ...
- ios7中的多任务
转自:http://onevcat.com/2013/08/ios7-background-multitask/ WWDC 2013 Session笔记 - iOS7中的多任务 iOS7的后台多任务特 ...
- [ios-必看] WWDC 2013 Session笔记 - iOS7中的多任务【转】
感谢:http://onevcat.com/2013/08/ios7-background-multitask/ http://www.objc.io/issue-5/multitasking.htm ...
- UITableView使用过程中可能遇到的问题
前言:记录一些UITableView使用过程中可能遇到的问题 环境:Xcode9 解决UITableViewStyleGrouped类型的TableView的cell距离顶部有距离的问题: table ...
- iOS7中的多任务 - Background Fetch,Silent Remote Notifications,Background Transfer Service
转自:http://onevcat.com/2013/08/ios7-background-multitask/ 在IOS 7 出来不就,公司内部也组织了一次关于IOS 7 特性的的分享,今天看见on ...
- iOS7中的多任务I
[改变了后台任务的运行方式] 在iOS6和之前的系统中,系统在用户退出应用后,如果应用正在执行后台任务的话,系统会保持活跃状态直到后台任务完成或者是超时以后,才会进入真正的低功耗休眠状态. 而在iOS ...
- iOS7中的ViewController切换
转自:https://onevcat.com/2013/10/vc-transition-in-ios7/ iOS 7 SDK之前的VC切换解决方案 在深入iOS 7的VC切换效果的新API实现之前, ...
- iOS7中计算UILabel中字符串的高度
iOS7中计算UILabel中字符串的高度 iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UIL ...
随机推荐
- sklearn 绘制roc曲线
from sklearn.metrics import roc_curve, auc import matplotlib as mpl import matplotlib.pyplot as plt ...
- dynamic解析Http xml格式响应数据
继续上一篇 构建RESTful风格的WCF服务 ,咱已经把服务端的数据和服务准备好了,客户端调用 wcf rest接口后如何解析xml?下面使用dynamic关键字解析来至于WCF REST XML响 ...
- Nginx配置日志格式记录cookie
Nginx配置日志格式记录cookie1. 一般用来做UV统计,或者获取用户token等. 配置方式: 在nginx的配置文件中有个变量:$http_cookie来获取cookie的信息.配置方式很 ...
- 基于json-lib-2.2.2-jdk15.jar的JSON解析工具类大集合
json解析之前的必备工作:导入json解析必须的六个包 资源链接:百度云:链接:https://pan.baidu.com/s/1dAEQQy 密码:1v1z 代码示例: package com.s ...
- urlrewriteFilter condition----reference
src:http://tuckey.org/urlrewrite/manual/2.6/ <condition> element An element that lets you choo ...
- 一个在linux环境执行io操作的bug
今天项目有了一个奇葩的要求...是什么呢 后台上传了视频后,解析其中的时长,和预览图,并拼接在一起,然而,之东西并不是太麻烦,很快写好了,在本地测试后也没有问题,嗯,发布到测试环境后,一个jar包报错 ...
- [CQOI 2018]社交网络
Description 题库链接 求 \(n\) 个点以 \(1\) 为根的有向生成树个数. \(1\leq n\leq 250\) Solution 我终于会 \(\texttt{Matrix-Tr ...
- C#基础知识回顾--BackgroundWorker介绍
简介 BackgroundWorker是.net里用来执行多线程任务的控件,它允许编程者在一个单独的线程上执行一些操作.耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 始终 ...
- python可变对象与不可变对象
可变/不可变对象定义 不可变对象 该对象所指向的内存中的值不能被改变.当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一个新的地址,变量再指向这个新的地址. 可 ...
- IntelliJ IDEA创建spring-boot项目
开发环境: jdk版本:JDK8 maven版本:maven-3.5.2 开发工具:Itellij IDEA 2017.1 前提条件:已安装以上软件并配置好jdk和maven的环境变量 创建步骤: 点 ...