iOS 10的消息应用程序导航栏会在你推入/弹出对话时增加/减少高度(并带有平滑过渡效果)。
通常我使用 sizeThatFits:
来创建较高的自定义导航栏,但该导航栏会在导航控制器中推入/弹出视图控制器时保留高度。
像消息应用程序那样,如何只为某些视图控制器设置较高的导航栏,并跨导航序列进行管理呢? 谢谢!
iOS 10的消息应用程序导航栏会在你推入/弹出对话时增加/减少高度(并带有平滑过渡效果)。
通常我使用 sizeThatFits:
来创建较高的自定义导航栏,但该导航栏会在导航控制器中推入/弹出视图控制器时保留高度。
像消息应用程序那样,如何只为某些视图控制器设置较高的导航栏,并跨导航序列进行管理呢? 谢谢!
UIView.beginAnimations(nil, context: nil)
self.frame = navFrame
UIView.commitAnimations()
extension UINavigationBar {
func applyHeight(_ height: CGFloat, animated: Bool = true) {
var navFrame = self.frame
navFrame.size.height = height
if animated {
UIView.beginAnimations(nil, context: nil)
self.frame = navFrame
UIView.commitAnimations()
} else {
self.frame = navFrame
}
}
}
class ViewControllerA: UIViewController {
override func loadView() {
super.loadView()
title = "A"
view.backgroundColor = .blue
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
navigationController?.navigationBar.isTranslucent = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func showController() {
navigationController?.pushViewController(ViewControllerB(), animated: true)
}
}
class ViewControllerB: UIViewController {
override func loadView() {
super.loadView()
title = "B"
view.backgroundColor = .red
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.applyHeight(100)
super.viewWillAppear(animated)
}
override func willMove(toParentViewController parent: UIViewController?) {
if parent == nil { // here you know that back button was tapped
navigationController?.navigationBar.applyHeight(44)
}
super.willMove(toParentViewController: parent)
}
}
当您滑动以弹出时,跳转标题是可见的,但我个人认为这是一个小问题 :)
希望它能帮助您,也许有人可以改进这个实现。 当然,我仍然会尝试找出如何使其更好 :)
这里是github存储库。 请使用navigation_bar_height
分支。
titleLabel
和backButtonTitleLabel
会跳到顶部。我猜你也需要对它们应用一些偏移量。 - E-RiddielayoutSubviews
方法,只是将所有子视图上移。 - Codus
UINavigationBar
扩展创建了一个设置height
值并使用该值计算sizeThatFits
框架的方法。现在我可以使用此扩展在viewWillAppear
上更新height
,通过在动画循环中设置navigationBar
高度并调用navigationBar.sizeToFit()
。 当推入时效果很好,但是当返回时效果不太好。尝试在viewWillDissapear
或上一个控制器的viewWillAppear
上以相同的方式重置框架。 - alexbumbu