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

时间:2022-08-22 22:51:36

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

解决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;

}

效果:

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

注意:此种问题只有在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. &lbrack;ios-必看&rsqb; 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. github desktop 在线安装失败解决方案

    1.问题概述 我的OS是Windows8.1 64位,尝试安装github desktop,始终安装失败:进度到50%左右就炸了.提示说:网络出错.(我100M电信,网络出错?我一直都在上网好吗). ...

  2. 自己搭建Wifi Pineapple Mark V

    创业搞得自己很累,不过一切都是值得的.抽空写下文章,确实好久未更新了. 前段时间由于项目需要,所以就折腾了下wifi pineapple.时间间隔有点久,根据回忆记录下. 淘宝货:TP-Link TL ...

  3. DIH中添加不同的数据源

    需求:从mysql数据库中读取一个知识记录,从记录表中的字段值中获取一个文件路径,读取xml文件,xml文件中可能包含多个文档内容.建立索引. xml文件样例: <?xml version=&q ...

  4. Wad Manager的下载和运行

    关于WAD版的软件 WAD版的软件分为两种: 独立安装版:程序完全安装在Wii的内置存储空间上,可以在Wii的主界面直接启动软件. Forwarder版:程序安装在Wii的内置存储空间上,同时在SD卡 ...

  5. react-native 自己搭建热更新服务器

    使用到的框架是 react-native-update react-native-update-cli 这个应该执行热更新的时候的终端命令. 通过这个,自己搭建一个热更新的服务器.

  6. Linux 在下面MATLAB下载

    网上找了很长一段时间都没有很好的连接,或者是一个普通的软件, 在这里,给大家一个连接: ed2k://|file|[%E7%9F%A9%E9%98%B5%E5%AE%9E%E9%AA%8C%E5%AE ...

  7. java中equals方法和contentEquals方法区别

    java中,String类里提供了两种字符串的比较方式(算上“==”应该是三种) String line1 = new String("0123456789"); String l ...

  8. HYSBZ1036-树链剖分-点权

    树链剖分,点权,单点更改,路径查询.学树链剖分下面这个博文不错 http://blog.csdn.net/y990041769/article/details/40348013 线段树必须写的很熟练才 ...

  9. python实现中文字符繁体和简体中文转换-乾颐堂

    需求:把中文字符串进行繁体和简体中文的转换: 思路:引入简繁体处理库,有兴趣的同学可以研究一下内部实现,都是python写的 1.下载zh_wiki.py及langconv zh_wiki.py:ht ...

  10. 如何将查出的日期Data类型以Json格式输出到前端

    方法一 在返回的实体的属性中加上注解 // 创建时间    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private ...