你需要打电话
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)