iOS7 兼容适配 如何判断版本号

很多时候我们需要做不同版本的适配,所以首先要进行版本选择

  • 方式一
<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
<span class="line-number" style="color: inherit !important;">6</span>
<span class="line-number" style="color: inherit !important;">7</span>
<span class="line-number" style="color: inherit !important;">8</span>
<span class="line-number" style="color: inherit !important;">9</span>
<span class="line-number" style="color: inherit !important;">10</span>
<span class="line-number" style="color: inherit !important;">11</span>
<span class="line-number" style="color: inherit !important;">12</span>
<span class="line-number" style="color: inherit !important;">13</span>
NSUInteger DeviceSystemMajorVersion();
NSUInteger DeviceSystemMajorVersion() {
      static NSUInteger _deviceSystemMajorVersion = -1;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
       _deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion]
           componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
      });

      return _deviceSystemMajorVersion;
}
 #define MY_MACRO_NAME (DeviceSystemMajorVersion() < 7)
}
  • 方式二
<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}

新的barTintColor

iOS7 中新增了barTintColor 来取代原有的 tintColor,
现在barTintColor表示对背景色的修改,而原有的tintColor只修改对应bar上的按钮

自定义 UIBarButtonItem 偏移

在iOS7中自定义的 UIBarButtonItem 所有的item向中间偏移了5个像素,所以需要修改alignmentRectInsets来适配,
例如

<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
<span class="line-number" style="color: inherit !important;">6</span>
<span class="line-number" style="color: inherit !important;">7</span>
<span class="line-number" style="color: inherit !important;">8</span>
<span class="line-number" style="color: inherit !important;">9</span>
<span class="line-number" style="color: inherit !important;">10</span>
<span class="line-number" style="color: inherit !important;">11</span>
<span class="line-number" style="color: inherit !important;">12</span>
<span class="line-number" style="color: inherit !important;">13</span>
<span class="line-number" style="color: inherit !important;">14</span>
- (UIEdgeInsets)alignmentRectInsets {
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0,0);

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
        if (self.isLeft) {
            insets = UIEdgeInsetsMake(0, 5.0f, 0, 0);
        }
        else { // IF_ITS_A_RIGHT_BUTTON
            insets = UIEdgeInsetsMake(0, 0, 0, 5.0f);
        }
    }

    return insets;
}

edgesForExtendedLayout 是什么

edgesForExtendedLayout是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向。 因为iOS7鼓励全屏布局,它的默认值很自然地是UIRectEdgeAll,四周边缘均延伸,就是说,如果即使视图中上有navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域。

如果把视图做如下设置,那么视图就不会延伸到这些bar的后面了.

<span class="line-number" style="color: inherit !important;">1</span>
self.edgesForExtendedLayout = UIExtendedEdgeNone;

一些相关的属性

<span class="line-number" style="color: inherit !important;">1</span>
automaticallyAdjustsScrollViewInsets = YES

在iOS7中如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖.

<span class="line-number" style="color: inherit !important;">1</span>
extendedLayoutIncludesOpaqueBars = YES

最后一个介绍的新属性是extendedLayoutIncludesOpaqueBars,这个属性指定了当Bar使用了不透明图片时,视图是否延伸至Bar所在区域,默认值时NO。

grouped 的变化

在iOS7 中UITableView的grouped延伸至两边变成了通栏.里面的控件用autolayout来兼容

PS: 一个不是问题的问题,在XCode5中如果是一个xib创建的grouped的tableView,将不会正常显示,需要重新初始化,例如

<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
<span class="line-number" style="color: inherit !important;">6</span>
<span class="line-number" style="color: inherit !important;">7</span>
<span class="line-number" style="color: inherit !important;">8</span>
<span class="line-number" style="color: inherit !important;">9</span>
<span class="line-number" style="color: inherit !important;">10</span>
<span class="line-number" style="color: inherit !important;">11</span>
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        self.tableView.backgroundView = nil;
        self.tableView.backgroundColor = TOP_VIEW_COLOR;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
    }
    return self;
}

一些情况下 window.tintColor 的使用

如果不为窗体指定着色,则会使用系统默认的颜色。

默认情况下,视图的着色是nil,意味着视图使用父级的着色。也就是说哪怕你不设置着色的话,视图也总能够获取到一个色值。

总的来说,最好在视图还没有显示到屏幕上之前指定它的着色。想让视图继承上个层级的着色的话,就将着色设置为nil。

info.plist 中的View controller-based status bar appearance

如果你想要隐藏status bar, 或者用原来的方式修改status bar的颜色.在info.plist中增加这个属性,并且设置为NO

UITextView 的改变

这个请参考 stackover flow

消失的search bar

PS: 另一个不是问题的问题,把searchBar 当做tableView的header的时候,如果含有UISearchDisplayController,有时候会导致search bar消失. 目前没有找到很好的办法,目前重新初始化可以解决.

<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
<span class="line-number" style="color: inherit !important;">6</span>
<span class="line-number" style="color: inherit !important;">7</span>
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    [self.searchController setDelegate:self];
    [self.searchController setSearchResultsDataSource:self];
    [self.searchController setSearchResultsDelegate:self];
}

新的UITableViewCellScrollView

在iOS7之前UITablleViewCell中得contentView得superView就是UITableViewCell。但是在iOS7得时候,contentView得superView确实UITableViewCellScrollView.可以用以下代码来获取

<span class="line-number" style="color: inherit !important;">1</span>
<span class="line-number" style="color: inherit !important;">2</span>
<span class="line-number" style="color: inherit !important;">3</span>
<span class="line-number" style="color: inherit !important;">4</span>
<span class="line-number" style="color: inherit !important;">5</span>
<span class="line-number" style="color: inherit !important;">6</span>
<span class="line-number" style="color: inherit !important;">7</span>
<span class="line-number" style="color: inherit !important;">8</span>
<span class="line-number" style="color: inherit !important;">9</span>
<span class="line-number" style="color: inherit !important;">10</span>
<span class="line-number" style="color: inherit !important;">11</span>
<span class="line-number" style="color: inherit !important;">12</span>
<span class="line-number" style="color: inherit !important;">13</span>
<span class="line-number" style="color: inherit !important;">14</span>
<span class="line-number" style="color: inherit !important;">15</span>
<span class="line-number" style="color: inherit !important;">16</span>
@implementation UIView (GetCellFromContentviewSubview)
- (UITableViewCell *)getCellFromContentviewSubview
{
    if ([[[self superview] superview] isKindOfClass:[UITableViewCell class]]) {
        return (UITableViewCell *)[[self superview] superview];
    }
    else if ([[[[self superview] superview] superview] isKindOfClass:[UITableViewCell class]]) {
        return (UITableViewCell *)[[[self superview] superview] superview];
    }
    else{
         NSLog(@"something Panic happens");
    }
    return nil;
}

@end
时间: 2024-08-03 15:09:24

iOS7 兼容适配 如何判断版本号的相关文章

通用javascript代码判断版本号是否在版本范围之间_javascript技巧

通用判断版本号是否在两者之间,也可以搭配判断是否大于某版本号,小于取反即可 PS:需确保版本规范一致,比如都是.号分割的n位版本号 var APP = {}; //判断指定版本是否在版本范围之间,需确保版本规范一致;比如 (..,..,..) APP.betweenVersion = function(curr,start,end,separator){ if(curr == start || curr == end){ return true; } var separator = separa

iPhone屏幕尺寸、分辨率及适配

1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS 2.4 inches (62.1 mm) 4.5 inches (115.5 mm) 3.5-inch   320x480 @1x 320x480 163 4(s) 2.31 inches (58.6 mm) 4.5 inches (115.2 mm) 3.5-inch 320x480 @2x 64

json写的jsp加载json写的js页面为什么有时候会不兼容?

问题描述 json写的jsp加载json写的js页面为什么有时候会不兼容? json写的jsp加载json写的js页面为什么有时候会不兼容? 解决方案 不知道你说的不兼容是什么意思,是不兼容开发环境还是不兼容浏览器?建议你用框架,它们已经考虑了兼容适配的问题.

图文讲解如何解决App的iOS 7顶部状态栏适配问题_IOS

首先说明下,ios7中,由于status bar不再占用单独的20px,如果app需要同时支持ios7和ios6.1以下,那就需要适配下了,适配开始: 先看用xcode新建项目后 IOS7和IOS6上的的运行效果: ps:一个empty application 里面+了一个rootcontroller,作为window的根控制器,view里面放了一个tableview; 是不是遇到的IOS7的新问题,状态栏跟tableview重叠了,OK,看见这个不想看到的结果,下面我们就开始正式的解决掉这个招

JS魔法堂:浏览器模式和文档模式怎么玩?

一.前言   从IE8开始引入了文档兼容模式的概念,作为开发人员的我们可以在开发人员工具中通过"浏览器模式"和"文档模式"(IE11开始改为"浏览器模式"改成 更贴切的"用户代理字符串")品味一番,它的出现极大地方便了苦逼的前端攻城狮们适配各版本的IE,但jser们也不能完全信任它,因为它只是提供尽可能 的文档模式模拟而已.   本篇大部分内容来源于官方解说:http://msdn.microsoft.com/library/

10天学安卓-第五天

原文:10天学安卓-第五天 经过前几天的练习,相信大家已经对如何做出一个简单的界面有了初步的了解,并且已经做出来一个还不错的天气列表了. 今天大家稍事休息,要练习的内容比较少,着重学习一些理论知识,先理清几个概念. Android系统架构 Android系统本质上是Linux系统,但相对于Linux系统,主要在驱动.性能.内存管理.设备管理等一些部分做了比较多的改动,以更适用于移动设备. 从上图可以看到,Android系统架构为四层,分别是Linux内核.系统运行库.应用程序框架以及应用层,每一

ym——android源码大放送(实战开发必备)

文件夹 PATH 列表 卷序列号为 000A-8F50 E:. │  javaapk.com文件列表生成工具.bat │  使用说明.txt │  免费下载更多源码.url │  目录列表.txt │   ├─android web应用 │      jqmDemo_static.zip │      jqmMobileDemo-master.zip │      jqmMobileDemo1_1-master.zip │      Location1014.rar │ ├─anko │    

java-Android sqlite更新,本人新手菜鸟

问题描述 Android sqlite更新,本人新手菜鸟 本人看"第一行代码"这本书,看到sqlite升级数据库这个地方,发现比如要新增一张表,可以通过onUpgrade()方法中添加db.execSQL(**)语句完成,那么,为什么onCreate()方法也要添加这条语句? 解决方案 如果在新的手机上安装,第一次安装时,执行的是onCreate中的,安装过后,后面更新才执行onUpgrade 解决方案二: onUpgrade()是升级的时候才调用 解决方案三: 覆盖第一次安装和重复安

通用宏

// // Common.h // CloudShopping // // Created by sixiaobo on 14-7-8. // Copyright (c) 2014年 com.Uni2uni. All rights reserved. // #ifndef CloudShopping_Common_h #define CloudShopping_Common_h /*! * @brief 全局能用宏 * * @author huangyibiao */ // @{ // @nam