第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Swift中使用UI_USER_INTERFACE_IDIOM()檢測當前設備

在Swift中使用UI_USER_INTERFACE_IDIOM()檢測當前設備

慕慕森 2019-11-22 11:12:45
UI_USER_INTERFACE_IDIOM()在iPhone和iPad之間檢測到的Swift 相當于什么?Use of unresolved identifier在Swift中編譯時出現(xiàn)錯誤。
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經(jīng)驗 獲得超5個贊

使用Swift時,您可以使用enum UIUserInterfaceIdiom,定義為:


enum UIUserInterfaceIdiom : Int {

    case unspecified


    case phone // iPhone and iPod touch style UI

    case pad // iPad style UI

}

因此,您可以將其用作:


UIDevice.current.userInterfaceIdiom == .pad

UIDevice.current.userInterfaceIdiom == .phone

UIDevice.current.userInterfaceIdiom == .unspecified

或使用Switch語句:


    switch UIDevice.current.userInterfaceIdiom {

    case .phone:

        // It's an iPhone

    case .pad:

        // It's an iPad

    case .unspecified:

        // Uh, oh! What could it be?

    }

UI_USER_INTERFACE_IDIOM() 是一個Objective-C宏,定義為:


#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)

另外,請注意,即使在使用Objective-C時,UI_USER_INTERFACE_IDIOM()也僅在定位iOS 3.2及更低版本時才需要該宏。部署到iOS 3.2及更高版本時,可以[UIDevice userInterfaceIdiom]直接使用。


查看完整回答
反對 回復 2019-11-22
?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

Swift 2.0和iOS 9和Xcode 7.1


// 1. request an UITraitCollection instance

let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom


// 2. check the idiom

switch (deviceIdiom) {


case .Pad:

    print("iPad style UI")

case .Phone:

    print("iPhone and iPod touch style UI")

case .TV: 

    print("tvOS style UI")

default:

    print("Unspecified UI idiom")


}

Swift 3.0和Swift 4.0


// 1. request an UITraitCollection instance

let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom


// 2. check the idiom

switch (deviceIdiom) {


case .pad:

    print("iPad style UI")

case .phone:

    print("iPhone and iPod touch style UI")

case .tv: 

    print("tvOS style UI")

default:

    print("Unspecified UI idiom")

}

使用UITraitCollection。通過UITraitEnvironment協(xié)議的traitCollection屬性公開了iOS trait環(huán)境。以下類別采用了此協(xié)議:


UIScreen

UIWindow

UIViewController

UIPresentationController

UIView


查看完整回答
反對 回復 2019-11-22
  • 3 回答
  • 0 關注
  • 1261 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號