2016-02-19 96 views
1

我想讓我的swift iPhone/iPad應用程序在分割視圖或iPhone上運行時發生幾件事情。基於這個答案(Detect if app is running in Slide Over or Split View mode in iOS 9),我已經想出瞭如何在Objective-C中做到這一點。我沒有使用分割視圖控制器(如果分割視圖處於活動狀態,則會在小數情況下產生壓力)。是否可以在現場時間檢查應用程序窗口的寬度?檢查應用程序是否處於分割視圖swift

基本上,我只想在窗口寬度低於某個值時在我的應用中發生某些事情。

+0

我從來沒有使用拆分視圖進行測試,但檢查UIScreen.mainScreen()。邊界是否有效。 – UlyssesR

+0

尺碼類別不同。 – nhgrif

+0

那麼,@UlyssesR有什麼想法? – owlswipe

回答

0

據我所知,我在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() 
} 

} 
相關問題