导航栏大标题-动画问题

时间:2019-12-06 16:50:42

标签: ios swift xcode uinavigationbar

我在导航栏上使用大标题,当我点击一个单元格以转到下一个控制器时,大标题具有奇怪的动画(如下面的gif所示)。它不会立即消失。

我尝试了以下解决方案,但没有采取任何措施(https://www.morningswiftui.com/blog/fix-large-title-animation-on-ios13

enter image description here

我的代码:

在第一个View Controller上:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        navigationItem.title = "New Order"
        navigationController?.navigationBar.prefersLargeTitles = true
}

在第二个视图控制器上(带有大标题):

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        navigationItem.title = "Menu"
        self.navigationController?.navigationBar.prefersLargeTitles = false
}

编辑:

Fabio的答案是解决方案,但现在我还有另一个问题:

当我点击一个单元格时,导航栏的一部分是黑色的(如下图所示)

enter image description here

5 个答案:

答案 0 :(得分:4)

尝试在第一个View Controller上插入:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationItem.title = "New Order"
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode =  .always
 }

并在Second View Controller上:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationItem.title = "Menu"
    navigationItem.largeTitleDisplayMode = .never
}

答案 1 :(得分:1)

将此扩展程序复制到设置导航栏:

extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithOpaqueBackground()
    navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
    navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
    navBarAppearance.backgroundColor = backgoundColor
    navigationController?.navigationBar.standardAppearance = navBarAppearance
    navigationController?.navigationBar.compactAppearance = navBarAppearance
    navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

    navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
    navigationController?.navigationBar.isTranslucent = false
    navigationController?.navigationBar.tintColor = tintColor
    navigationItem.title = title

} else {
    // Fallback on earlier versions
    navigationController?.navigationBar.barTintColor = backgoundColor
    navigationController?.navigationBar.tintColor = tintColor
    navigationController?.navigationBar.isTranslucent = false
    navigationItem.title = title
}}}

使用方法,请调用viewWillAppear

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "YourTitle", preferredLargeTitle: true)

现在在didSelectRow中使用pushVievController调用第二个控制器,如下所示:

let controller = YourViewController()
    navigationController?.pushViewController(controller, animated: true)

在第二个控制器的viewDidLoad中,将大标题设置为false,如下所示:

navigationItem.largeTitleDisplayMode = .never

如果要点亮内容,请在info.plist中将基于ViewController的状态栏设置为NO。 如果您不希望largeTitles将其设置为false 在iOS 13上测试过,希望对您有所帮助:) 现在你可以了:)

答案 2 :(得分:1)

第二期: 在SceneDelegate中:var window: UIWindow? 在功能场景中放入以下行:

window?.backgroundColor = .yourColor

像这样:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let _ = (scene as? UIWindowScene) else { return }
    window?.backgroundColor = .white
}

设置您想要的颜色就可以了:)

答案 3 :(得分:0)

尝试在Second View Controller中使用viewDidLoad代替viewWillAppear

答案 4 :(得分:0)

因此,您希望第一个视图控制器具有较大的标题,然后将以下代码添加到viewWillAppear(animated:)的第一个视图控制器中

self.navigationItem.largeTitleDisplayMode = .automatic
self.navigationController?.navigationBar.prefersLargeTitles = true

您想,您的下一个视图控制器标题不大,然后将以下代码添加到viewWillAppear(animated:)的下一个视图控制器中

self.navigationItem.largeTitleDisplayMode = .never

结论是,您只需要将self.navigationController?.navigationBar.prefersLargeTitles设置为true即可,只要您希望视图控制器需要在显示视图的开始处显示大标题