更改UINavigationBar的外观

时间:2019-03-18 13:17:58

标签: ios swift uinavigationbar

我的应用程序中有几个视图控制器,它们应该具有透明的导航栏。由于这些视图控制器的数量可以动态变化,因此我将原始的UINavigationBar细分为自己的GDSNavigationBar。它具有UINavgiationController的扩展名,可以从任何视图控制器访问此自定义导航栏,并且具有更改外观的功能。但是,当我尝试更改外观时,导航栏会发疯。

这是我的自定义导航栏:

enum GDSNavigationBarStyle {
    case whiteOpaque
    case transparent
    case transparentWithTitle
}

class GDSNavigationBar: UINavigationBar {

    public var customBarStyle: GDSNavigationBarStyle {get {return aCustomBarStyle}}
    private var aCustomBarStyle: GDSNavigationBarStyle = .whiteOpaque

    public func setCustomBarStyle(_ style: GDSNavigationBarStyle) {
        if style == aCustomBarStyle {return}

        switch style {
        case .whiteOpaque:
            setWhiteOpaqueStyle()
            break
        case .transparent:
            setTransparentStyle()
            break
        case .transparentWithTitle:
            setTransparentWithTitleStyle()
            break
        }
    }

    private func setWhiteOpaqueStyle() {
        barStyle = .default
        barTintColor = UIColor.white
        backgroundColor = UIColor.white
        tintColor = UIApplication.shared.keyWindow?.tintColor
        shadowImage = UIImage()
        setBackgroundImage(nil, for: .default)

        let attributes = [NSAttributedString.Key.foregroundColor : UIColor.black]

        titleTextAttributes = attributes
        largeTitleTextAttributes = attributes
    }

    private func setTransparentStyle() {
        barStyle = .black
        barTintColor = UIColor.clear
        backgroundColor = UIColor.clear
        tintColor = UIColor.white
        shadowImage = UIImage()
        setBackgroundImage(UIImage(), for: .default)

        let attributes = [NSAttributedString.Key.foregroundColor : UIColor.clear]

        titleTextAttributes = attributes
        largeTitleTextAttributes = attributes

    }

导航栏拒绝更改其样式。套用attributes后,标题和大标题也会消失。

0 个答案:

没有答案
相关问题