예는 ViewWillApear의 값을 설정하고 맑고 투명 탐색 모음 및 정상적인 색상의 네비게이션 바있는을위한 viewWillDisappear 함수의 값을 재설정해야합니다.
일부 열거 형 값에 따라 값을 설정하거나 재설정 할 수있는 UINavigationController의 Extension을 사용할 수 있습니다. UIImage에서 내비게이션 막대 및 그림자 이미지의 배경 이미지로 적용 할 이미지 색상을 만들 수있는 확장 기능을 만듭니다.
enum NavigationBarMode {
case normal
case clear
}
extension UINavigationController {
func themeNavigationBar(mode: NavigationBarMode) {
self.navigationBar.isTranslucent = true
switch mode {
case .normal:
let image = UIImage.fromColor(.white)
navigationBar.setBackgroundImage(image, for: .default)
navigationBar.shadowImage = UIImage.fromColor(.lightGray)
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)]
case .clear:
let image = UIImage()
navigationBar.setBackgroundImage(image, for: .default)
navigationBar.shadowImage = image
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)]
}
}
}
extension UIImage {
static func fromColor(_ color: UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
}
지금은 viewWillApear
에 themeNavigationBar(mode: .transparent)
를 호출하고 다시 viewWillDisappear
에 themeNavigationBar(mode: .normal)
를 호출 할 수 있습니다. 또한 다른 navigationController에서 일반적인 탐색 모음 설정이있는 경우 themeNavigationbar (mode : .normal)을 호출 할 수 있습니다.
당신이 당신을 변경하기 전에 viewWillAppear``에서 인스턴스 속성에 현재 값을 저장해야하고, 그들을 복원하는 것
https://github.com/MoZhouqi/KMNavigationBarTransition https://github.com/DanisFabric/RainbowNavigation
을 사용하실 수 있습니다 일부 타사 cocoapods 있습니다 'viewWillDisappear'. – matt