iOS小技巧---swift 判断IOS版本及适配



operatingSystemVersion

为了更复杂的版本比较,operatingSystemVersion能够被直接检查。将它和Swift模式比较和switch语句组合,可以使得代码更简洁。


let os = NSProcessInfo().operatingSystemVersion

switch  (os.majorVersion, os.minorVersion, os.patchVersion) {
case  (8, _, _):
     println( "iOS >= 8.0.0" )
case  (7, 0, _):
     println( "iOS >= 7.0.0, < 7.1.0" )
case  (7, _, _):
     println( "iOS >= 7.1.0, < 8.0.0" )
default :
     println( "iOS < 7.0.0" )

}





UIDevice systemVersion

可惜的是,新NSProcessInfo API目前并不是特别有用,因为它们在iOS 7上不生效。

作为替代,可以使用systemVersion属性UIDevice来进行检查:

switch  UIDevice.currentDevice().systemVersion.compare( "8.0.0" , options: NSStringCompareOptions.NumericSearch) {
case  .OrderedSame, .OrderedDescending:
     println( "iOS >= 8.0" )
case  .OrderedAscending:
     println( "iOS < 8.0" )
}



适配屏幕 

        switch UIDevice.currentDevice().systemVersion.compare("7.0.0", options: NSStringCompareOptions.NumericSearch) {

        case .OrderedSame, .OrderedDescending:

            println("iOS >= 7.0")

            self.edgesForExtendedLayout = UIRectEdge.None

            self.automaticallyAdjustsScrollViewInsets = false

        case .OrderedAscending:

            println("iOS < 7.0")

        }


转载于:https://my.oschina.net/u/2331935/blog/484324

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值