iOS 7 & iOS 6适配问题

1.状态栏 StatusBar的适配

iOS 7.0以上版本 view的高度包括状态栏 所以要进行版本的适配
方法1:改变view的frame view的高度从-20开始

iOS7view默认是全屏模式,状态栏的高度也加在了view的高度上,例如iOS7之前iphone5self.view.frame.size.height = 548,在iOS7中就是568了,在iOS7navigationbar是半透明的,statusbar则是全透明的,这样一来,原来的程序用xcode5+iOS7sdk上编译后运行就会出现问题了。

(一)没有导航栏的viewController适配方法

UIButton * btn = nil;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//        self.view.frame = CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 20); // 此处不起作用
        
        btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 140, 30)];
    }else {
        btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
    }
    
    [btn setTitle:@"状态栏适配" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [self.view addSubview:btn];

2.导航栏 NavigationBar的适配


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.edgesForExtendedLayout = NO; // 取消导航的影响
}

 

导航栏的默认字体为黑色,但是如果导航栏背景为深色的话就需要来改动字体的颜色了,这时需要重写viewControllersetTitle方法:

#pragma mark - 重写setTitle方法
- (void)setTitle:(NSString *)title {

    [super setTitle:title];
    
    UILabel * titleView = (UILabel *)self.navigationItem.titleView;
    
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        
        titleView.backgroundColor = [UIColor clearColor];
        titleView.shadowColor = [UIColor colorWithWhite:0.1 alpha:0.5];
        titleView.font = [UIFont boldSystemFontOfSize:20];
        titleView.textColor = [UIColor whiteColor];
        
        self.navigationItem.titleView = titleView;
    }
    
    titleView.text = title;
    [titleView sizeToFit]; // 大小自适应
}

这样就可以根据需要来设置导航栏的字体、颜色、大小、阴影了。如果在每个界面都这么写的话会非常麻烦,可以为viewController增加一个类别方法或者用宏定义来实现。

 

在IOS7下,如果不设置navigationBar的背景图片,而设置[navigationBar setBarStyle:UIBarStyleBlackTranslucent];可以获取默认的黑色毛玻璃效果

 

3.UITableView的适配

1.分割线的位置

iOS7之前tableviewd的分割线默认情况下是居左的,宽度和tableview的宽度一样,但是在iOS7中,分割线默认向右移动了10几个像素

#pragma mark - 创建TableView
- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // iOS 7之后tableview的分割线向右移了10px 需要设置tableView的separatorInset
    // iOS 6没有此方法
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = [UIColor purpleColor];
    
    [self.view addSubview:_tableView];
}


如图:

想要调整为居左显示,需要设置一下tableview的属性,    tableview.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

但是该属性是在iOS7中才有的,使用时需要判断一下系统的版本号,否则在iOS7之前的设备上运行会出现错误。

2.设置透明色的方法

 

iOS7之前设置tableview为透明色的时候,只要设置tableview.backgroundColor=[UIColor ClearColor]就行了,但是在iOS7中发现只设置tableview的背景色没有达到透明的效果,还需要设置cell.backgroundColor = [UIColor ClearColor]才可以。

 

、UIScrollView

在iOS7中scrollview滚动的时候,上下都可以滚动,即使contensize的高度和内容的高度一样也是如此,设置属性self.automaticallyAdjustsScrollViewInsets = NO就可以了,具体原因还没有搞明白。


注:判断系统版本的宏定义:#define IS_IOS7_SYSTEM [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0

 

五、唯一标识符

 

iOS6之后,苹果禁用了禁用了UIDeviceuniqueIdentifier方法,所以获取设备唯一标识的方法采用了获取Mac地址然后MD5加密,但是,在iOS7中发现,该方法统一返回02:00:00:00:00:00,所以用做设备的标识符已经没有意义。经过调研、查阅资料和各种方案对比分析,采用了ADID,以下是ADID的特点及使用方法。

 

 

提供方

苹果API

用途

广告服务

系统支持

iOS6iOS7

使用方法

1.     首先在target->Buidl Phases->Link Binary With Libraries中添加AdSupport.framework

2.     在需要使用的文件里包含ASIdentifierManager.h文件

3.     调用advertisingIdentifier 实例方法

代码:NSString *adId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];

返回值发生改变的情况

1.     设置->通用->还原->抹掉所有内容和设置

2.     iOS6: 设置->通用->关于本机->广告->还原广告标识符

3.     iOS7: 设置->隐私->广告->还原广告标识符

返回值不发生改变的情况

1.     设置->通用->还原->还原所有设置

2.     卸载应用程序后重新安装

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。