Swift UINavigationBar在滚动/转换时更改为意外颜色
我有一个带有透明标题栏的UIView,当用户滚动时,背景慢慢淡入白色Swift UINavigationBar在滚动/转换时更改为意外颜色,swift,uikit,core-graphics,core-animation,Swift,Uikit,Core Graphics,Core Animation,我有一个带有透明标题栏的UIView,当用户滚动时,背景慢慢淡入白色 func imageFromColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetC
func imageFromColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContext(size)
image?.draw(in: rect)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
override func viewDidLoad() {
super.viewDidLoad()
// remove nav bar bottom border
navigationController?.navigationBar.shadowImage = UIImage()
// set nav bar button color
let image = imageFromColor(color: UIColor(red: 255, green: 255, blue: 255, alpha: 0.0), size: CGSize(width: 1, height: 1))
navigationController?.navigationBar.setBackgroundImage(image, for: .default)
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black.withAlphaComponent(0.0)]
当用户滚动时,我会检测他们在滚动中的位置,并相应地更新导航栏
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var alpha = (scrollView.contentOffset.y / 200)
alpha = alpha > 1.0 ? 1.0 : alpha
if let listName = listName, let navBar = navigationController?.navigationBar {
let height = listName.frame.maxY - navBar.frame.maxY - 3.0
if scrollView.contentOffset.y > 0 && scrollView.contentOffset.y >= height, !titleShowing {
titleShowing = true
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.titleLabel?.alpha = 1
self.titleTopConstraint?.constant = -34.0
self.titleLabel?.layoutIfNeeded()
}, completion: nil)
} else if scrollView.contentOffset.y < height, titleShowing {
self.titleShowing = false
self.titleLabel?.alpha = 0
UIView.animate(withDuration: 0.4, delay: 0, options: .curveEaseIn, animations: {
self.titleTopConstraint?.constant = 0
self.titleLabel?.layoutIfNeeded()
}, completion: nil)
}
}
let image = imageFromColor(color: UIColor(red: 255, green: 255, blue: 255, alpha: alpha), size: CGSize(width: 1, height: 1))
navigationController?.navigationBar.setBackgroundImage(image, for: .default)
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black.withAlphaComponent(alpha)]
}
正如您在下面的gif中所看到的,导航栏跳转为白色而不是清晰的,如果我返回到上一个视图控制器并向上滚动,曾经清晰的导航栏将从白色变为黑色,我无法理解这一点。我在整个VC中没有.black
的实例
因此,要重申,有两个问题:
1) 滚动后,VC A推送至VC B,VC B的导航条跳转为白色,而不是以清晰开始
2) 滚动后,VC A推到VC B,用户返回VC A并向上滚动,导航栏从白色变为黑色,而不是从白色变为透明
我很乐意在这里帮忙
谢谢
Gif示例:
Edit:经过一些调查,似乎由于VC A
和VC B
都有imageFromColor
函数,在从A转换到B之后,调用A中函数的版本,因为在转换之后技术上调用了viewditscroll
(奇怪)。这就是为什么它会更新为白色而不是清晰
let image = imageFromColor(color: UIColor(red: 255, green: 255, blue: 255, alpha: 0.0), size: CGSize(width: 1, height: 1))
navigationController?.navigationBar.setBackgroundImage(image, for: .default)