【问题标题】:How to change the background image of navigation bar with animation?如何用动画改变导航栏的背景图片?
【发布时间】:2017-06-28 11:05:42
【问题描述】:

我正在使用我们所有人都知道的动画,动画以更改导航栏的背景图像,延迟时间很短,以将其从半透明(我将其设为默认)更改为(蓝色主题)。

UIView.animate(withDuration:0.5, 
               delay: 0, 
               option: UIViewAnimationOptions.curveEaseOut, 
               animations: {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }, completion: nil)

但是动画没有效果,但是导航栏的背景图片变成功了,不知道为什么!!

有什么想法吗?

【问题讨论】:

  • 没有改变颜色的代码你设置的只是背景图像,没有图像。
  • @NiravD 更新

标签: ios swift3 uinavigationbar uiviewanimation


【解决方案1】:

您需要在动画块中调用layoutIfNeeded(),例如

UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 
    self.navigationController?.navigationBar.barTintColor = .blue
    self.navigationController?.navigationBar.layoutIfNeeded()
}, completion: nil)

更改背景图片的更新

这段代码对我有用:

let animation = CATransition()
animation.duration = 0.5
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.type = kCATransitionFade

navigationController?.navigationBar.layer.add(animation, forKey: nil)

UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your-image-name"), for: .default)
}, completion: nil)

【讨论】:

  • 谢谢,但什么也没发生。 ':(
  • 您需要更改backgroundImage 还是可以像上面的示例代码一样更改颜色?我现在没有时间进行调查,但是在更改图像时,这里有一个明显的 Objective-C 解决方案:stackoverflow.com/questions/10823068/…
  • background Image,Objective-C 的另一个答案不起作用!
  • 我已为我的答案添加了更新。看看这是否适合你(它适合我)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多