解決UITableView在iOS7中UINavigationController裡的頂部留白問題

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   for   sp   

解決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以上才會出現

 

解決UITableView在iOS7中UINavigationController裡的頂部留白問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.