2017-08-24 91 views
1

更新:我最後隱藏了默認的導航欄並添加了一個與導航欄相同的UIView。這可能聽起來不太好,但不是修補到UINavigationBar這很好。如何在Swift中增加自定義UINavigationBar類的高度


這是我的自定義,我已經創造增加導航條的高度在我的應用程序UINavigationBar類。它不適合我。這是code

class PPBaseNavigationBar: UINavigationBar { 

    ///The height you want your navigation bar to be of 
    static let navigationBarHeight: CGFloat = 83.0 

    ///The difference between new height and default height 
    static let heightIncrease: CGFloat = navigationBarHeight - 44 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     initialize() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     initialize() 
    } 

    private func initialize() { 
     let shift = PPBaseNavigationBar.heightIncrease/2.0 

     ///Transform all view to shift upward for [shift] point 
     self.transform = CGAffineTransform(translationX: 0, y: -shift) 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     let shift = PPBaseNavigationBar.heightIncrease/2.0 

     ///Move the background down for [shift] point 
     var classNamesToReposition: Array<String>? 
     if #available(iOS 10.0, *) { 
      classNamesToReposition = ["_UIBarBackground"] 
     } else { 
      classNamesToReposition = ["_UINavigationBarBackground"] 
     } 

     for view: UIView in self.subviews { 
      if (classNamesToReposition?.contains(NSStringFromClass(view.classForCoder)))! { 
       let bounds: CGRect = self.bounds 
       var frame: CGRect = view.frame 
       frame.origin.y = bounds.origin.y + shift - 20.0 
       frame.size.height = bounds.size.height + 20.0 
       view.frame = frame 
      } 
     } 
    } 

    override func sizeThatFits(_ size: CGSize) -> CGSize { 
     let amendedSize:CGSize = super.sizeThatFits(size) 
     let newSize:CGSize = CGSize.init(width: amendedSize.width, height: PPBaseNavigationBar.navigationBarHeight) 
     return newSize; 
    } 
} 

所有方法被調用除了override func sizeThatFits(_ size: CGSize) -> CGSize {...}不知道,爲什麼呢?

+0

您是否試圖通過編程方式添加導航欄? –

+0

創建導航控制器參考,然後在NavBar上初始化它的新參考.. 您可以初始化導航欄與框架 –

+1

嗨馬拉夫,感謝您的答覆兄弟。我用UIStoryboard&編程方式嘗試過,但沒有幫助。我只是通過替換默認導航欄添加了一個'UIView'。這是我的應用的一個很好的解決方案。 – Hemang

回答

3
It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly. 

Modifying the Navigation Bar Object Directly

可以使用自定義視圖導航欄。定製視圖按您的要求(例如,改變高度)和隱藏默認的導航欄作爲

self.navigationController?.setNavigationBarHidden(true, animated: false) 
0

更改您的代碼像下面

open override func sizeThatFits(_ size: CGSize) -> CGSize { 
     let amendedSize:CGSize = super.sizeThatFits(size) 
     let newSize:CGSize = CGSize.init(width: amendedSize.width, height: PPBaseNavigationBar.navigationBarHeight) 
     return newSize; 
    } 

或許對你來說工作。

1

嘗試這樣的,這個工作對我來說在斯威夫特3:

extension UINavigationBar { 

    override open func sizeThatFits(_ size: CGSize) -> CGSize { 
     return CGSize(width: UIScreen.main.bounds.size.width, height: "Your custom height") 
    } 

} 


// MARK: - View life cycle methods 

class DVNavigationController: UINavigationController { 

    override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 
     navigationBar.frame.size.height = "Your custom height" 
    } 

} 

最後分配這個自定義UINavigationController類到您的navigationController

1

layoutSubviews也可以重寫func sizeThatFits(_ size:CGSize)。 所以不要擔心,如果它不是sizeThatFits調用。我檢查了layoutSubviews導航欄高度正在改變。

for view: UIView in self.subviews { 
     if (classNamesToReposition?.contains(NSStringFromClass(view.classForCoder)))! { 
      let bounds: CGRect = self.bounds 
      var frame: CGRect = view.frame 
      frame.origin.y = bounds.origin.y + shift - 20.0 
      frame.size.height = bounds.size.height + 150.0 
      view.frame = frame 
     } 
    }