iOS DeveloperiOS点点滴滴

iOS Review | UIAppearance夜间主题配置

2017-09-21  本文已影响120人  清無
最终效果

要求:

UIApplication Key Window
// 主色
UIApplication.shared.delegate?.window??.tintColor = mainColor

UINavigaitonBar

barStyle = .default barStyle = .black backIndicatorImage backIndicatorTransitionMaskImage 正确的backIndicatorImage和backIndicatorTransitionMaskImage组合模式
INavigationBar.appearance().barStyle = barStyle
UINavigationBar.appearance().setBackgroundImage(navigationBarBackgroundImage, for: .default)
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "backArrow")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "backArrowMaskFixed")
UITaBar
selectionIndicatorImage
UITabBar.appearance().barStyle = barStyle
UITabBar.appearance().backgroundImage = tabBarBackgroundImage
let tabIndicator = UIImage(named: "tabBarSelectionIndicator")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets: .init(top: 0, left: 2, bottom: 0, right: 2))
UITabBar.appearance().selectionIndicatorImage = tabIndicator
UISegmentedControl
UISegmentedControl
let controlBgImage = UIImage(named: "controlBackground")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets: .init(top: 3, left: 3, bottom: 3, right: 3))
let controlSelectedBgImage = UIImage(named: "controlSelectedBackground")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets: .init(top: 3, left: 3, bottom: 3, right: 3))
UISegmentedControl.appearance().setBackgroundImage(controlBgImage, for: .normal, barMetrics: .default)
UISegmentedControl.appearance().setBackgroundImage(controlSelectedBgImage, for: .selected, barMetrics: .default)
UIStepper
UIStepper
let controlBgImage = UIImage(named: "controlBackground")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets: .init(top: 3, left: 3, bottom: 3, right: 3))
UIStepper.appearance().setBackgroundImage(controlBgImage, for: .normal)
UIStepper.appearance().setBackgroundImage(controlBgImage, for: .highlighted)
UIStepper.appearance().setBackgroundImage(controlBgImage, for: .disabled)
UIStepper.appearance().setDecrementImage(UIImage.init(named: "fewerPaws")!, for: .normal)
UIStepper.appearance().setIncrementImage(UIImage.init(named: "morePaws")!, for: .normal)
UISlider
UISlider
UISlider.appearance().setThumbImage(UIImage(named: "sliderThumb"), for: .normal)
UISlider.appearance().setMaximumTrackImage(UIImage(named: "maximumTrack")?
    .resizableImage(withCapInsets:UIEdgeInsets(top: 0, left: 0.0, bottom: 0, right: 6.0)), for: .normal)
UISlider.appearance().setMinimumTrackImage(UIImage(named: "minimumTrack")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets:UIEdgeInsets(top: 0, left: 6.0, bottom: 0, right: 0)), for: .normal)
UIProgressView
UIProgressView
UIProgressView.appearance().trackImage = UIImage(named: "controlBackground")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets: .init(top: 3, left: 3, bottom: 3, right: 3))
UIProgressView.appearance().progressImage = UIImage(named: "maximumTrack")?
    .withRenderingMode(.alwaysTemplate)
    .resizableImage(withCapInsets:UIEdgeInsets(top: 0, left: 0.0, bottom: 0, right: 6.0))
UISwitch
UISwitch on
UISwitch.appearance().onTintColor = .red
UISwitch.appearance().thumbTintColor = mainColor
其他

一个夜间主题Demo

定义一个Theme枚举
enum Theme: Int {
    case `default`, dark, graphical
    
    private enum Keys: String{
        case selected = "ThemeSelected"
    }
}
static var current: Theme {
        return Theme.init(rawValue: UserDefaults.standard.integer(forKey: Keys.selected.rawValue)) ?? .default
    }
var mainColor: UIColor{
        switch self {
        case .default:
            return .blue
        case .dark:
            return .orange
        case .graphical:
            return .darkGray
        }
    }
    
    var barStyle: UIBarStyle{
        switch self {
        case .default, .graphical:
            return .default
        case .dark:
            return .black
        }
    }
    
    var navigationBarBackgroundImage: UIImage?{
        return self == .graphical ? UIImage.init(named: "navBarBackground") : nil
    }
    
    var tabBarBackgroundImage: UIImage?{
        return self == .graphical ? UIImage.init(named: "tabBarBackground") : nil
    }
    
    var backgroundColor: UIColor{
        switch self {
        case .default, .graphical:
            return .white
        case .dark:
            return UIColor(white: 0.4, alpha: 1)
        }
    }
    
    var textColor: UIColor{
        switch self {
        case .default, .graphical:
            return .black
        case .dark:
            return .white
        }
    }
func apply(){
    UserDefaults.standard.set(rawValue, forKey: Keys.selected.rawValue)
    UserDefaults.standard.synchronize()
    
    updateMainColor()
    
    updateNavigationBarAppearance()
    
    updateTableViewAppearance()
    
    updateLabelAppearance()
    
    updateTabBarAppearance()
    
    updateSegmentedControlAppearance()
    
    updateStepperAppearance()
    
    updateSliderAppearance()
    
    updateProgressAppearance()
    
    updateSwitchAppearance()
    
    updateActivityIndicatorAppearance()
}
private func updateNavigationBarAppearance(){
        UINavigationBar.appearance().barStyle = barStyle
        UINavigationBar.appearance().setBackgroundImage(navigationBarBackgroundImage, for: .default)
        UINavigationBar.appearance().backIndicatorImage = UIImage(named: "backArrow")
        UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "backArrowMaskFixed")
    }
使用主题
@IBAction func changeTheme(_ sender: UISegmentedControl) {
// 存储并设置主题
    Theme(rawValue: sender.selectedSegmentIndex)!.apply()
// TODO: 让主题立即生效
}
// TODO: 让主题立即生效
let window = UIApplication.shared.delegate?.window
window??.subviews.forEach{
    view in
    view.removeFromSuperview()
    window??.addSubview(view)
}
自动设置夜间主题
// 北京
private let solar = Solar(latitude: 39.9047253699, longitude: 116.4072154982)!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    initializeTheme()
    
    return true
}

func initializeTheme(){
    if solar.isDaytime {
        Theme.current.apply()
        scheduleThemeTimer()
    }
    else{
        Theme.dark.apply()
    }
}
func scheduleThemeTimer(){
    let timer = Timer.init(fire: solar.sunset!, interval: 0, repeats: false) { [weak self] _ in
        Theme.dark.apply()
        
        self?.window?.subviews.forEach{
            view in
            view.removeFromSuperview()
            self?.window?.addSubview(view)
        }
    }
    
    RunLoop.main.add(timer, forMode: .commonModes)
}
Github

https://github.com/BackWorld/NightThemeDemo

如果对你有帮助,别忘了加个 关注 或 点个 哦😁

上一篇 下一篇

猜你喜欢

热点阅读