Detect if app is running in Slide Over or Split View mode in iOS 9

后端 未结 15 2482
死守一世寂寞
死守一世寂寞 2020-12-04 15:31

In iOS 9, is it possible to detect when an app is running in iOS 9\'s Slide Over or Split View mode?

I\'ve tried reading through Apple\'s documentation on iOS 9 mult

15条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 16:18

    And I'm really late to the party! But nonetheless, here's a simple, swifty solution to the problem. Using let width = UIScreen.mainScreen().applicationFrame.size.width we can detect the width of my app's window, and then have things occur when it is smaller than a certain number (i.e. on iPhone screen or in split view), useful to make different things happen on smaller screens. To have the computer check the width over and over again, we can run an NSTimer every hundredth of a second, then do stuff if the width is higher/lower than something.

    Some measurements for you (you have to decide what width to make stuff occur above/below): iPhone 6S Plus: 414.0mm
    iPhone 6S: 375.0mm
    iPhone 5S: 320.0mm
    iPad (portrait): 768.0mm
    iPad (1/3 split view): 320.0mm
    iPad Air 2 (1/2 split view): 507.0mm
    iPad (landscape): 1024.0mm

    Here's a code snippet:

    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()
    }
    
    }
    

    I hope this can help anyone who comes peeking!

提交回复
热议问题