swift5中的UITabBar.appearance()。barStyle崩溃

时间:2019-08-28 21:16:55

标签: ios swift background-color uitabbar

我实现了“暗模式”,我需要将TabBar样式从默认更改为暗,然后相反。

我的应用程序崩溃了!

我该怎么办?

  

2019-08-28 21:42:57.276131 + 0200得分[1817:344811] *正在终止应用   由于未捕获的异常'NSInvalidArgumentException',原因:'Bar   UITabBar不支持的样式。支持的栏样式为   UIBarStyleDefault和UIBarStyleBlack'            * 第一个调用堆栈:            (0x19ef4aab8 0x19e14fd00 0x19ee502f4 0x1cbbd5f08 0x19ef52414 0x19ee4d0bc 0x1cbfb610c 0x1cc7f9e44 0x1cbfb6098 0x1cbfb03b8   0x1cbfaf7d8 0x1cc822ee4 0x1cc822e54 0x1cbbd8790 0x1cc822d58   0x1cc822174 0x1cbbda498 0x1cc821edc 0x1cc8152ac 0x1cc815170   0x1cc824d20 0x1cc3ac​​68c 0x1cc3ac​​ab0 0x1cc3bd80c 0x1cc36e090   0x1cc3739fc 0x1cbc05e04 0x1cbc0e7e0 0x1cbc05a6c 0x1cbc06388   0x1cbc045d0 0x1cbc0428c 0x1cbc08fbc 0x1cbc09e14 0x1cbc08e70   0x1cbc0de60 0x1cc371f38 0x1cbf54650 0x1a190dfd0 0x1a191831c   0x1a1917a6c 0x1004f477c 0x1004f833c 0x1a194b18c 0x1a194ae08   0x1a194b404 0x19eedab54 0x19eedaad0 0x19eeda38c 0x19eed5060   0x19eed4964 0x1a1115d8c 0x1cc375758 0x1000a96ec 0x19e990fd8)            libc ++ abi.dylib:以类型为NSException的未捕获异常终止

enum Theme: Int {
case Light, Dark

var barStyle: UIBarStyle {
    switch self {
    case .Light:
        return .default
    case .Dark:
        return .blackTranslucent
    }
}
//...
}

class ThemeManager {
static func applyTheme(theme: Theme) {
    if(theme == .Dark){
        UserDefaults.standard.set(true, forKey: "darkMode")
    } else {
        UserDefaults.standard.set(false, forKey: "darkMode")
    }
    UserDefaults.standard.synchronize()

    let sharedApplication = UIApplication.shared
    sharedApplication.delegate?.window??.tintColor = theme.buttonColor

    UINavigationBar.appearance().barStyle = theme.barStyle
    UITabBar.appearance().barStyle = theme.barStyle

    UITabBar.appearance().barTintColor = theme.labelColor
    UINavigationBar.appearance().tintColor = theme.labelColor

    //...

}
}

1 个答案:

答案 0 :(得分:0)

异常终止状态的原因:

  

受支持的栏样式为UIBarStyleDefault和 UIBarStyleBlack

看来,我们要做的就是将barStyle修改为:

var barStyle: UIBarStyle {
    switch self {
    case .Light:
        return .default
    case .Dark:
        return .black
    }
  }