【问题标题】:UINavigationBar weird color change animation when dismissing UIViewController关闭 UIViewController 时 UINavigationBar 奇怪的颜色变化动画
【发布时间】:2017-02-16 10:32:16
【问题描述】:

我有一个FirstViewController 和一个SecondViewController。他们的UINavigationBar 有不同的颜色。当我显示SecondViewController 时,颜色会逐渐消失。我用模拟器录制动画和慢动画。

但是,当我从 SecondViewController 回到 FirstViewController 时,颜色没有动画,一切都立即改变。

这就是我在SecondViewController 中为UINavigationBar 设置代码的方式。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let navBar = self.navigationController?.navigationBar {

        navBar.barStyle = UIBarStyle.black
        navBar.barTintColor = NavBarColor.red
        navBar.backgroundColor = NavBarColor.red
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        navBar.isTranslucent = false
        navBar.tintColor = .white
    }
}

在我的FirstViewController类中,我创建了一个structNavBarSettings,并保存了UINavigationBar的信息。然后我将它们应用到viewWillAppear

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let navBar = self.navigationController?.navigationBar,
        let navBarSettings = self.navBarSettings {

        navBar.barStyle = navBarSettings.barStyle
        navBar.barTintColor = navBarSettings.barTintColor
        navBar.backgroundColor = navBarSettings.backgroundColor
        navBar.titleTextAttributes = navBarSettings.titleTextAttributes
        navBar.isTranslucent = navBarSettings.isTranslucent
        navBar.tintColor = navBarSettings.tintColor

    }
}

我也试过更改SecondViewControllerviewWillDisappear中的UINavigationBar信息,但效果一样。

我也尝试设置backgroundColor,但它也没有改变任何东西。

如何让第二个动画像第一个一样工作?

更新

转至SecondViewController 是一种善意的表现。

我只是用self.performSegue(withIdentifier: "SecondViewControllerSegue", sender: nil)调用它

我没有在后退按钮上添加任何自定义代码,它是默认的UINavigationController 实现。

【问题讨论】:

  • 对不起,我误解了你的问题,我已经删除了答案。您是否尝试过手动设置值,而不是简单地在 FirstViewController 的 viewWillAppear 方法中从 navBarSettings 设置?
  • 是的,但没有区别
  • 您能否用代码更新您的问题,说明您如何推送 SecondViewController 以及如何弹出它?
  • @Sneak 我更新了问题。对你有帮助吗?
  • 是的,我以前试过。 Clinton 找到了解决方案,感谢您的帮助 Sneak :)

标签: ios swift uinavigationbar


【解决方案1】:

尝试用自定义后退按钮替换后退按钮并向其添加操作。

let backButton = UIButton()
backButton.addTarget(self, action: #selector(self.backButtonClicked), for: UIControlEvents.touchUpInside)
navBar.navigationItem.leftBarButtonItem = barButton 

func backButtonClicked() {
   // try implementing the same thing here but with the self.navigationController?.popViewController(animated: true)
}

【讨论】:

  • 谢谢!:) self.navigationController?.popViewController(animated: true) 部分成功了
猜你喜欢
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2017-02-04
  • 2016-12-15
  • 2014-01-19
相关资源
最近更新 更多