I have a UIView with a transparent title bar, and when the user scrolls, the background slowly fades into white.
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()
navigationController?.navigationBar.shadowImage = UIImage()
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)]
When the user scrolls, I discover where they are in the scroll and updates the navigation bar accordingly.
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)]
}
Everything works fine as long as I scrolled down to the place where the Nav bar will be white, and then tap the item that pushes to the presentation controller, which must again be displayed Nav panel with the same setting in viewDidLoad:
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)
gif , Nav Bar , , , , - , . .black VC.
, 2 :
1) VC A VC B , VC B Nav Bar ,
2) VC A VC B, VC A , Nav Bar ,
.
!
Gif: 
: , VC A VC B imageFromColor, B A A , viewDidScroll - (). , , , .