iPhoneX指示条(visual indicator)

阅读更多

 

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

 

API

iPhone X每个控制器底部都有一个指示条(visual indicator),并且默认是一直显示的。但是有时候我们想让他隐藏,比如全屏播放视频时,还有苹果是提供这个功能的:

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.
    //控制器重写
    //控制器是tab或navigation等容器时可以重现,返回当前选中的控制器,使用该子控制器的设置,返回nil则自己的prefersHomeIndicatorAutoHidden()会生效
    @available(iOS 11.0, *)
    open func childViewControllerForHomeIndicatorAutoHidden() -> UIViewController?  

    
    // Controls the application's preferred home indicator auto-hiding when this view controller is shown.
    //控制器重写
    //返回true:控制器操作时会显示,没有操作时会自动隐藏掉,
    //返回false:指示条会一直显示,也是默认显示逻辑
    @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.
    //控制器调用
    //更新指示条显示逻辑
    @available(iOS 11.0, *)
    open func setNeedsUpdateOfHomeIndicatorAutoHidden()
}

##例子

  • 比如播放器全屏时需要指示器自动隐藏(详细代码地址):
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
    }


}
  • 在tab或navigation等容器中实现,其实类似于容器中调整控制器方向:
class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    // 返回nil, 则自己的prefersHomeIndicatorAutoHidden()中的设置会生效
    // 这里返回当前选中的控制器,使用该子控制器的设置
    override func childViewControllerForHomeIndicatorAutoHidden() -> UIViewController? {
        return selectedViewController
    }
    
    // 注意: 此方法中的设置生效的前提是上面这个方法返回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
    }
}

你可能感兴趣的:(iPhoneX指示条(visual indicator))