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

简介: <div id="article_details" class="details" style="margin:20px; font-family:Arial,Console,Verdana,'Courier New'"> <div id="article_content" class="article_content" style="margin:20px 0px 0px; font-

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

  • 方式一
<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
目录
相关文章
|
7月前
|
Linux Android开发 iOS开发
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
120 0
|
前端开发 Android开发 iOS开发
new Date兼容iOS和Android
new Date兼容iOS和Android
193 0
|
1月前
|
iOS开发
SwiftUI适配iOS16导航控制器引起的闪退
SwiftUI适配iOS16导航控制器引起的闪退
31 0
|
1月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
25 0
|
7月前
|
小程序 开发工具 Android开发
Donut多端框架小程序打包适配ios和安卓app
腾讯新出了一个 Donut 多端框架,可以直接将微信小程序转成 ios 和 安卓 app,小程序开发者工具里也集成了 app 相关升级、调试和打包的功能,终于可以一套代码开发出3个客户端了!
171 0
Donut多端框架小程序打包适配ios和安卓app
|
8月前
|
安全 前端开发 开发工具
iOS12、iOS11、iOS10、iOS9常见适配
iOS12、iOS11、iOS10、iOS9常见适配
151 0
|
9月前
|
小程序 JavaScript API
支付宝小程序集成mqtt兼容IOS和安卓
支付宝小程序集成mqtt兼容IOS和安卓
160 0
|
9月前
|
小程序 JavaScript API
支付宝微信小程序连接蓝牙兼容IOS和安卓(开源)
支付宝微信小程序连接蓝牙兼容IOS和安卓(开源)
114 0
|
9月前
|
iOS开发
倒计时15分钟-兼容ios手机效果demo(整理)
倒计时15分钟-兼容ios手机效果demo(整理)
|
9月前
|
小程序 iOS开发
uniapp中IOS端小程序底部黑线适配的方法(整理)
uniapp中IOS端小程序底部黑线适配的方法(整理)