UINavigationItem

2019/9/11 posted in  iOS

纵观Apple的官方应用程式的导航栏你就会发现在iOS11之后导航栏的高度会自动变化,文字的大小以及位置也会随之变化。
这是iOS11之后的新特性,苹果是使用给UINavigationItem添加扩展的方法增加的新功能,这也给了我们新的一种思路,为了不去修改之前代码的原则上添加新的功能的方式。

extension UINavigationItem {
    public enum LargeTitleDisplayMode : Int {
        /// Automatically use the large out-of-line title based on the state of the previous item in the navigation bar. An item with largeTitleDisplayMode=Automatic will show or hide the large title based on the request of the previous navigation item. If the first item pushed is set to Automatic, then it will show the large title if the navigation bar has prefersLargeTitles=YES.
        case automatic

        
        /// Always use a larger title when this item is top most.
        case always

        
        /// Never use a larger title when this item is top most.
        case never
    }
}
  • largeTitie的两种展现形式如下所示

屏幕快照 2018-12-05 下午1.23.25 屏幕快照 2018-12-05 下午1.23.29

设置方式

  • xib设置

    • never 一直都是小标题
    • automatic 自动切换
    • always 一直都是大标题

屏幕快照 2018-12-05 下午1.30.58

  • 代码设置

    因为这是新增加的属性所以要做判断
     if #available(iOS 11.0, *) {
    self.navigationItem.largeTitleDisplayMode = .automatic
    } else {
    // Fallback on earlier versions
    };