iPhoneX indicator bar (visual indicator)

 

 Original address: https://github.com/easyui/blog/blob/master/iOS/2017-10-20-iPhoneX%E6%8C%87%E7%A4%BA%E6%9D%A1.md

 

API

The iPhone X has a visual indicator at the bottom of each controller, which is always displayed by default. But sometimes we want him to hide, such as when playing video in full screen, and Apple provides this function:

extension UIViewController {

    // Override to return a child view controller or nil. If non-nil, that view controller's home indicator auto-hiding will be used. If nil, self is used. Whenever the return value changes, -setNeedsHomeIndicatorAutoHiddenUpdate should be called. 
    // Controller rewrite 
    // When the controller is a container such as tab or navigation, it can be reproduced, return the currently selected controller, use the settings of the sub-controller, and return nil, then your own preferencesHomeIndicatorAutoHidden() will take effect 
    @available ( iOS  11.0 , * )
     open  func  childViewControllerForHomeIndicatorAutoHidden () -> UIViewController ?  

    
    // Controls the application's preferred home indicator auto-hiding when this view controller is shown. 
    // Controller override 
    // Return true: it will be displayed when the controller is in operation, and it will be automatically hidden when there is no operation, 
    // return false: indicate The bar will always be displayed, and it is also the default display logic 
    @available ( iOS  11.0 , * )
     open  func  prefersHomeIndicatorAutoHidden () ->  Bool

    
    // This should be called whenever the return values ​​for the view controller's home indicator auto-hiding have changed. 
    // Controller calls 
    // Update indicator display logic 
    @available ( iOS  11.0 , * )
     open  func  setNeedsUpdateOfHomeIndicatorAutoHidden ()
}

##example

  • For example, when the player is full screen, the indicator needs to be automatically hidden ( detailed code address ):
import  UIKit

open class EZPlayerFullScreenViewController: UIViewController {
    weak  var player: EZPlayer!

    // MARK: - Life cycle
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override open func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerControlsHiddenDidChange(_:)), name: NSNotification.Name.EZPlayerControlsHiddenDidChange, object: nil)
    }

    // MARK: - notification
    @objc func playerControlsHiddenDidChange(_ notifiaction: Notification) {
        self.statusBarHiddenAnimated = notifiaction.userInfo?[Notification.Key.EZPlayerControlsHiddenDidChangeByAnimatedKey] as? Bool ?? true
        self.setNeedsStatusBarAppearanceUpdate()
        if #available(iOS 11.0, *) {
            self.setNeedsUpdateOfHomeIndicatorAutoHidden()
        }
    }

    
    open override func prefersHomeIndicatorAutoHidden() -> Bool {
        return self.player.controlsHidden
    }


}
  • Implemented in a container such as tab or navigation, it is actually similar to adjusting the direction of the controller in the container:
class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    // Return nil, the settings in your own prefersHomeIndicatorAutoHidden() will take effect 
    // here returns the currently selected controller, use the settings of the child controller to 
    override  func  childViewControllerForHomeIndicatorAutoHidden () -> UIViewController ? {
         return selectedViewController
    }
    
    // Note: The setting in this method takes effect only if the above method returns nil 
    override  func  prefersHomeIndicatorAutoHidden () ->  Bool {
         return  true
    }
}
class NavagationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func childViewControllerForHomeIndicatorAutoHidden() -> UIViewController? {
        return self.viewControllers.last
    }
    
    override func prefersHomeIndicatorAutoHidden() -> Bool {
        return true
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326082366&siteId=291194637