2017-12-16 40 views
0

하나의보기 컨트롤러 탐색 바를 반투명하게 만들고 다른 속성을 변경하려고합니다.스위프트 4 하나의보기 컨트롤러에 대해서만 네비게이션 속성 설정

사용자가이 VC를 나가면 '정상'즉 AppDelegate에있는 항목으로 돌아가고 싶습니다.

viewWillDisappear에서 각 행을 재설정해야합니까? 그렇다면 기본 탐색 모음 설정으로 배경 이미지/그림자 이미지에 사용할 것은 무엇입니까?

// Make Nav Bar Translucent and Set title font/color 
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    self.navigationController?.navigationBar.shadowImage = UIImage() 
    self.navigationController?.navigationBar.isTranslucent = true 
    self.navigationController?.view.backgroundColor = .clear 
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)] 
+0

을 사용하실 수 있습니다 일부 타사 cocoapods 있습니다 'viewWillDisappear'. – matt

답변

0

예는 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! 
    } 
} 

지금은 viewWillApearthemeNavigationBar(mode: .transparent)를 호출하고 다시 viewWillDisappearthemeNavigationBar(mode: .normal)를 호출 할 수 있습니다. 또한 다른 navigationController에서 일반적인 탐색 모음 설정이있는 경우 themeNavigationbar (mode : .normal)을 호출 할 수 있습니다.

당신이 당신을 변경하기 전에 viewWillAppear``에서 인스턴스 속성에 현재 값을 저장해야하고, 그들을 복원하는 것

https://github.com/MoZhouqi/KMNavigationBarTransition https://github.com/DanisFabric/RainbowNavigation

+0

감사합니다. 와우는 그것에 포드를 찾는 것조차 생각하지 않았습니다. 나는 당신의 솔루션을 시도하고 있지만 그것은 네비게이션 바 모드라는 타입이 없다는 것을 말하고있다. – pmanning

+0

답변 수정, NavigationBarMode enum 추가 및 시도 – suhit