ios7版本适配问题总结(一)


一、状态栏statusBar

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

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

下面是以一个实例来说明:

1.首先创建一个测试工程,根视图是一个UIViewController,在view上添加一个buttonframe为(0010040),运行结果如图:

iOS7适配问题总结 - snowyshell - snowyshell的博客

button和状态栏重叠了,也就是说,在没有导航栏的情况下,代码运行在iOS7上,内容向上偏移了20px,通过查阅资料,找到两种解决方法:

1)设置self.view.bounds,代码如下:

iOS7适配问题总结 - snowyshell - snowyshell的博客

运行结果:

iOS7适配问题总结 - snowyshell - snowyshell的博客

这种方法需要更改每一个viewController,较为麻烦

2.设置window,在appdelegate.mapplication: didFinishLaunchingWithOptions:方法中添加代码如下:

iOS7适配问题总结 - snowyshell - snowyshell的博客

这种方法可以影响所有的subview。

运行之后发现状态上的内容看不到了:

iOS7适配问题总结 - snowyshell - snowyshell的博客

解决方法:在info.plist文件中添加View controller-based status bar appearance项,设置为NO。

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

现在将根视图改为UINavigationController,不设置任何属性,运行结果:

iOS7适配问题总结 - snowyshell - snowyshell的博客

可以看到button被导航栏覆盖了,只要在viewDidLoad中设置以下self.view的edgesForExtendedLayout属性就行了,self.edgesForExtendedLayout = UIRectEdgeNone;再运行一下:

iOS7适配问题总结 - snowyshell - snowyshell的博客


二、导航栏navigationBar

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

iOS7适配问题总结 - snowyshell - snowyshell的博客

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

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

三、UITableView
1.分割线的位置

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

iOS7适配问题总结 - snowyshell - snowyshell的博客

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

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

2.设置透明色的方法

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

四、唯一标识符

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.     卸载应用程序后重新安装

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

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


如何判断版本号

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

  • 方式一
<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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值