NavigationBarの背景をimageViewの上では透明に、imageViewの下までスクロールしたら変更する方法をメモ
// はじめにNavigationBarを透明に
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
// scrollViewのデリゲートメソッド
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// imageViewの下までスクロールしたら NavigationBarの色を変更
if scrollView.contentOffset.y > imageView.layer.frame.height - (navigationController?.navigationBar.frame.height)! - UIApplication.shared.statusBarFrame.height {
// NavigationBarの背景色変更
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.shadowImage = nil
} else {
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
}