简体   繁体   English

Swift 小到大导航栏标题跳跃式过渡

[英]Swift Small to Large Nav Bar Title Jumpy Transition

So on my first vc I have set the nav bar to a large title.所以在我的第一个 vc 上,我将导航栏设置为一个大标题。 Then I have a button which goes to a vc with a nav bar with a small title.然后我有一个按钮,它转到带有小标题的导航栏的 vc。

When I go back from my second vc to the first, it displays the small title for a bit then jumps down to the large title.当我从第二个 vc 返回到第一个时,它会显示小标题,然后跳到大标题。

Here is my code in the first vc bc its a tab view controller:这是我在第一个 vc 中的代码,它是一个选项卡视图控制器:

    override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.prefersLargeTitles = true
}

Here is the code for the second vc in the viewDidLoad():这是 viewDidLoad() 中第二个 vc 的代码:

    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Avenir-Black", size: 20)!]
    self.navigationController?.navigationBar.prefersLargeTitles = false

This is what I mean about the jumpy transition这就是我对跳跃过渡的意思

Thanks谢谢

On your second ViewController Try changing the NavBarPreference inside viewWillDisappear function. 在第二个ViewController上,尝试更改viewWillDisappear函数中的NavBarPreference。

Example: 例:

func viewWillDisappear(_ animated: Bool){ 
self.navigationController?.navigationBar.prefersLargeTitles = true
    }

So before going back to the first vc you change the NavBar preference first 因此,在返回第一个vc之前,请先更改NavBar首选项

Alternative Solution: Using Storyboard 替代解决方案:使用情节提要

You can click on the desired VC then click on its navBarItem then go to your right to properties and on Large Title Select Never, Always or Automatic from the dropdown list. 您可以单击所需的VC,然后单击其navBarItem,然后转到属性的右侧,然后在大标题上从下拉列表中选择“从不”,“始终”或“自动”。

Example

Short answer is to not rely on largeTitleDisplayMode = .automatic (the default value) and prefersLargeTitles = true/false but instead to be explicitly setting .always or .never with prefersLargeTitles = true (yes even when using never).简短的回答是不依赖于largeTitleDisplayMode = .automatic (默认值)和prefersLargeTitles = true/false ,而是明确设置.always.never并使用prefersLargeTitles = true (是的,即使在使用 never 时也是如此)。

You need to always have prefersLargeTitles to be true because of this from Apple doc:由于 Apple 文档中的这一点,您需要始终将 prefersLargeTitles 设为 true:

If the prefersLargeTitles property of the navigation bar is false, this property has no effect and the navigation item's title is always displayed as a small title.如果导航栏的prefersLargeTitles 属性为false,则该属性无效,导航项的标题始终显示为小标题。

This is an issue you have on iOS11/12 but on iOS13 it will be surfaced a bit differently.这是你在 iOS11/12 上遇到的一个问题,但在 iOS13 上它会有点不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM