解决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(0, [WxHxD statusBarAndNavigationBarHeight],
                                       [WxHxD screenWidth],
                                       [WxHxD screenHeight] - [WxHxD  statusBarAndNavigationBarHeight])];
    backView.layer.borderWidth = 2.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:24.f];
    [self titleTextAttributes:NCTitle];
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (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:18.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以上才会出现

目录
相关文章
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
124 0
|
缓存 算法 测试技术
iOS UITableView性能优化
iOS UITableView性能优化
iOS UITableView性能优化
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
268 0
|
iOS开发
iOS开发-关于UITableView去掉粘性的问题
iOS开发-关于UITableView去掉粘性的问题
57 0
|
iOS开发 开发者
iOS开发-简述UITableView中cell的重用问题
iOS开发-简述UITableView中cell的重用问题
162 0
|
程序员 iOS开发
iOS 列表 UITableView 提速指南
从08年到现在开发过的iOS应用不计其数了,但是面试很多人的时候,发现依然很多同学在最基本的列表控件上懂得不够深,下面就结合各方面的资料进行再一次讲解。 我们都知道纯代码是效率最高的,但是在开发成本上已经越来越不如使用Storyboard性价比高,速度快,所以本文试图结合UIStoryboard来描述一整套方案。
142 0
iOS 列表 UITableView 提速指南
|
iOS开发 开发者
iOS开发中行高灵活可变的UITableView的性能优化(二)
iOS开发中行高灵活可变的UITableView的性能优化
335 0
iOS开发中行高灵活可变的UITableView的性能优化(二)
|
开发者 iOS开发
iOS开发中行高灵活可变的UITableView的性能优化(一)
iOS开发中行高灵活可变的UITableView的性能优化
187 0
iOS开发中行高灵活可变的UITableView的性能优化(一)
|
iOS开发
iOS中UITableViewController自带的刷新控件
iOS中UITableViewController自带的刷新控件
126 0
iOS中UITableViewController自带的刷新控件
|
iOS开发 索引
iOS UITableView代理方法详解
iOS UITableView代理方法详解
159 0