據我所知,我在swift中製作了第一個不復雜的分割視圖檢測器(感謝Ulysses R.)! 因此,考慮到尤利西斯的建議,我使用let width = UIScreen.mainScreen().applicationFrame.size.width
來檢測應用窗口的寬度。爲了讓電腦一遍又一遍地檢查寬度,我每隔百分之一秒就運行一次NSTimer,然後在寬度高於/低於某個值時執行一些操作。
一些測量你(你必須決定什麼寬度,使東西出現上/下): iPhone 6S加:414.0
iPhone 6S:375.0
iPhone 5S:320.0
的iPad(縱向):768.0
ipad公司(1/3分割視圖):320.0
iPad的空氣2(1/2分割視圖):507.0
ipad公司(橫向):1024.0
這裏的一個代碼段:
class ViewController: UIViewController {
var widthtimer = NSTimer()
func checkwidth() {
var width = UIScreen.mainScreen().applicationFrame.size.width
if width < 507 { // The code inside this if statement will occur if the width is below 507.0mm (on portrait iPhones and in iPad 1/3 split view only). Use the measurements provided in the Stack Overflow answer above to determine at what width to have this occur.
// do the thing that happens in split view
textlabel.hidden = false
} else if width > 506 {
// undo the thing that happens in split view when return to full-screen
textlabel.hidden = true
}
}
override func viewDidAppear(animated: Bool) {
widthtimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "checkwidth", userInfo: nil, repeats: true)
// runs every hundredth of a second to call the checkwidth function, to check the width of the window.
}
override func viewDidDisappear(animated: Bool) {
widthtimer.invalidate()
}
}
我從來沒有使用拆分視圖進行測試,但檢查UIScreen.mainScreen()。邊界是否有效。 – UlyssesR
尺碼類別不同。 – nhgrif
那麼,@UlyssesR有什麼想法? – owlswipe