推送/弹出时类似于消息的更高/标准导航栏

17

iOS 10的消息应用程序导航栏会在你推入/弹出对话时增加/减少高度(并带有平滑过渡效果)。

通常我使用 sizeThatFits: 来创建较高的自定义导航栏,但该导航栏会在导航控制器中推入/弹出视图控制器时保留高度。

像消息应用程序那样,如何只为某些视图控制器设置较高的导航栏,并跨导航序列进行管理呢? 谢谢!


你找到解决方案了吗?我为UINavigationBar扩展创建了一个设置height值并使用该值计算sizeThatFits框架的方法。现在我可以使用此扩展在viewWillAppear上更新height,通过在动画循环中设置navigationBar高度并调用navigationBar.sizeToFit()。 当推入时效果很好,但是当返回时效果不太好。尝试在viewWillDissapear或上一个控制器的viewWillAppear上以相同的方式重置框架。 - alexbumbu
有进展了吗,伙计们? - Andrey Gagan
@user370773 你可以查看我的修改后的答案。我改变了导航栏高度动画的方式。它适用于滑动和返回按钮。 - kamwysoc
2个回答

7
非常有趣的问题。我花了一些时间在信息应用程序中实现类似的功能,以下是我的成果。

enter image description here

最后,我使用这个技巧在push/pop过程中动画化navigationBar的高度,并且也可以使用滑动手势进行pop操作。
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分支。


谢谢你的回答!这个方法看起来很不错!唯一的问题是,当过渡开始时,titleLabelbackButtonTitleLabel会跳到顶部。我猜你也需要对它们应用一些偏移量。 - E-Riddie
是的,你说得对,标题偏移也有点问题。我会尝试修复它。如果我的解决方案有新的进展,我会更新我的答案。 :) - kamwysoc
我曾经在自定义导航栏高度时覆盖了layoutSubviews方法,只是将所有子视图上移。 - Codus
1
似乎这个答案在iOS 11+上已经不再适用。 - Zimes

0

我认为现在您可以通过设置大标题始终来实现类似的功能:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接