iOS小技巧---swift 判斷IOS版本及適配

來源:互聯網
上載者:User

標籤:



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")

        }


iOS小技巧---swift 判斷IOS版本及適配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.