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

后端 未结 15 2466
死守一世寂寞
死守一世寂寞 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:00

    After much 'tinkering', I have found a solution for my App that may work for you:

    In AppDelegate.swift, create the following variable:

    var slideOverActive: Bool = false
    

    Then, in ALL of your view controllers, add the UIApplicationDelegate to the Class definition, create an appDelegate variable, and then add the below traitCollectionDidChange function:

    class myViewController: UIViewController, UIApplicationDelegate {
    
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    
    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
    
            let screenWidth = UIScreen.mainScreen().bounds.width
    
            if previousTraitCollection != nil {
                let horizontalSizeClass: Int = previousTraitCollection!.horizontalSizeClass.rawValue
    
                if screenWidth == 1024 || screenWidth == 768 { // iPad
    
                    if horizontalSizeClass == 2 { // Slide Over is ACTIVE!
    
                        appDelegate.slideOverActive = true
    
                    } else {
    
                        appDelegate.slideOverActive = false
    
                    }
                }
            }
        }
    
    }
    

    Then, wherever in your code you wish to check whether the slide-over is active or not, simply check:

    if appDelegate.slideOverActive == true {
    
        // DO THIS
    
    } else {
    
        // DO THIS
    
    }
    

    It's a bit of a workaround, but it works for me at the moment.

    Happy trails!

提交回复
热议问题