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

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

    Adding to @Tamas's answer:
    Here is the code snippet that will automatically maintain this flag irrespective of rotation.

     -(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
    {
     // simply create a property of 'BOOL' type
     isRunningInFullScreen = CGRectEqualToRect([UIApplication sharedApplication].delegate.window.frame, [UIApplication sharedApplication].delegate.window.screen.bounds);
    }
    
    0 讨论(0)
  • 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())
    }
    
    0 讨论(0)
  • 2020-12-04 16:14

    Just check if your window occupies the whole screen:

    BOOL isRunningInFullScreen = CGRectEqualToRect([UIApplication sharedApplication].delegate.window.frame, [UIApplication sharedApplication].delegate.window.screen.bounds);
    

    If this is false, then you're running in a split view or a slide over.

    Here is the code snipped which will automatically maintain this flag irrespective of rotation

    -(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
    {
     // simply create a property of 'BOOL' type
     isRunningInFullScreen = CGRectEqualToRect([UIApplication sharedApplication].delegate.window.frame, [UIApplication sharedApplication].delegate.window.screen.bounds);
    }
    
    0 讨论(0)
  • 2020-12-04 16:16

    The horizontal size class will be compact when in slide over or 33% split view. I don't think you can detect once you go to 50% or 66% though.

    0 讨论(0)
  • 2020-12-04 16:16

    I wrote a SizeClasser library based on @Michael Voccola's library.
    You can initialize it with the traitCollection of your view controller and detect split views as well as device specific orientation.
    So you could write your code specifically 1/3 horizontal split view or 1/3 portrait split view which UITraitCollection does not give you a way to detect them.
    https://github.com/cemolcay/SizeClasser

    0 讨论(0)
  • 2020-12-04 16:17

    Here is a simpler and less fragile way with no constants, that I use in an iPhone/iPad iOS app.

    This code also distinguishes between slide over and split view.

    I'm returning String values here for clarity, feel free to use enum values and to merge the two cases of fullscreen as suits your app.

    func windowMode() -> String {
      let screenRect = UIScreen.main.bounds
      let appRect = UIApplication.shared.windows[0].bounds
    
      if (UIDevice.current.userInterfaceIdiom == .phone) {
        return "iPhone fullscreen"
      } else if (screenRect == appRect) {
        return "iPad fullscreen"
      } else if (appRect.size.height < screenRect.size.height) {
        return "iPad slide over"
      } else {
        return "iPad split view"
      }
    }
    
    0 讨论(0)
提交回复
热议问题