Swift 4仅为一个视图控制器设置导航属性

Posted

技术标签:

【中文标题】Swift 4仅为一个视图控制器设置导航属性【英文标题】:Swift 4 Set Navigation Attributes for only one View Controller 【发布时间】:2017-12-16 04:01:44 【问题描述】:

我正在尝试只使一个视图控制器的导航栏半透明并更改其他一些属性。

在用户离开这个 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)]

【问题讨论】:

在进行更改之前,您需要将当前值保存到 viewWillAppear 中的实例属性中,然后在 viewWillDisappear 中恢复它们。 【参考方案1】:

是的,您必须在 ViewWillApear 中设置值并在 viewWillDisappear 函数中重置值以获得清晰的透明导航栏和正常颜色的导航栏。

您可以在 UINavigationController 上使用扩展,它可以根据一些枚举值设置/重置值。在 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) 进行重置。如果您有其他视图控制器的通用导航栏设置,您也可以调用 themeNavigationbar(mode: .normal)。

您可以使用一些第三方 cocoapods

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

【讨论】:

感谢您提供详细信息。哇甚至没有想到要在上面寻找一个豆荚。我正在尝试您的解决方案,但它说没有类型 NavigationBarMode? 编辑答案,添加 NavigationBarMode 枚举并尝试

以上是关于Swift 4仅为一个视图控制器设置导航属性的主要内容,如果未能解决你的问题,请参考以下文章

如何在使用当前模式打开另一个视图控制器后设置导航项的标题 - Swift

Swift - 远程通知和导航控制器流程

从标签栏控制器跳回导航控制器 - swift 4

在Swift中的导航栏中设置图像

Swift 导航栏颜色

UINavigation的外观设置和页面之间的跳转