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

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

    I recently had to determine display style of an application based including, not only if it changed to split view or slide-over, but also what portion of the screen was being utilized for the application (full, 1/3, 1/2, 2/3). Adding this to a ViewController subclass was able to solve the issue.

    /// Dismisses this ViewController with animation from a modal state.
    func dismissFormSheet () {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    private func deviceOrientation () -> UIDeviceOrientation {
        return UIDevice.currentDevice().orientation
    }
    
    private func getScreenSize () -> (description:String, size:CGRect) {
        let size = UIScreen.mainScreen().bounds
        let str = "SCREEN SIZE:\nwidth: \(size.width)\nheight: \(size.height)"
        return (str, size)
    }
    
    private func getApplicationSize () -> (description:String, size:CGRect) {
        let size = UIApplication.sharedApplication().windows[0].bounds
        let str = "\n\nAPPLICATION SIZE:\nwidth: \(size.width)\nheight: \(size.height)"
        return (str, size)
    }
    
    
    func respondToSizeChange (layoutStyle:LayoutStyle) {
        // Respond accordingly to the change in size.
    }
    
    enum LayoutStyle: String {
        case iPadFullscreen         = "iPad Full Screen"
        case iPadHalfScreen         = "iPad 1/2 Screen"
        case iPadTwoThirdScreeen    = "iPad 2/3 Screen"
        case iPadOneThirdScreen     = "iPad 1/3 Screen"
        case iPhoneFullScreen       = "iPhone"
    }
    
    private func determineLayout () -> LayoutStyle {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .iPhoneFullScreen
        }
        let screenSize = getScreenSize().size
        let appSize = getApplicationSize().size
        let screenWidth = screenSize.width
        let appWidth = appSize.width
        if screenSize == appSize {
            return .iPadFullscreen
        }
    
        // Set a range in case there is some mathematical inconsistency or other outside influence that results in the application width being less than exactly 1/3, 1/2 or 2/3.
        let lowRange = screenWidth - 15
        let highRange = screenWidth + 15
    
        if lowRange / 2 <= appWidth && appWidth <= highRange / 2 {
            return .iPadHalfScreen
        } else if appWidth <= highRange / 3 {
            return .iPadOneThirdScreen
        } else {
            return .iPadTwoThirdScreeen
        }
    
    }
    
    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        respondToSizeChange(determineLayout())
    }
    

提交回复
热议问题