GitHub地址git
pod 'ThemeStyleTools'
1.在AppDelegate裏面配置默認的主題色github
ThemeStyleTools.enable(defaultTheme: "#2F4F4F")
2.在切換主題色的地方更新主題swift
ThemeStyleTools.updateTheme(themeStr: colorStr!)
3.使用服務器
//修改UIView的背景顏色 ThemeStyleTools.cl_setupViewBackgroundColor(view: bgView) //修改UILabel的文字顏色 ThemeStyleTools.cl_setupViewLabelTextColor(label: label) //UIButton文字顏色normal ThemeStyleTools.cl_setupBtnNormalTextColor(btn: btn) //UIButton文字顏色selected ThemeStyleTools.cl_setupBtnSelectedTextColor(btn: btn) //UIButton圖片顏色normal ThemeStyleTools.cl_setupBtnNormalImgColor(btn: btn) //UIButton圖片顏色select ThemeStyleTools.cl_setupBtnSelectImgColor(btn: btn) //修改UIImageView的圖片顏色 ThemeStyleTools.cl_setupImageViewColor(img: bgView) //修改UISlider的顏色 ThemeStyleTools.cl_setupSliderColor(slider: slider) //修改UIPageControl的顏色 ThemeStyleTools.cl_setupPageControlColor(page: page) //修改UIProgressView的顏色 ThemeStyleTools.cl_setupProgressViewlColor(progress: progress) //修改UISwitch的顏色 ThemeStyleTools.cl_setupSwitchViewlColor(switchView: switchView) //修改UITextField的文字顏色 ThemeStyleTools.cl_setupTextFieldTextColor(textField: textField) //修改UISegmentedControl的顏色 ThemeStyleTools.cl_setupSegmentControllColor(segment: segment) //修改UITextView的文字顏色 ThemeStyleTools.cl_setupTextViewColor(textView: self.textView)
4.在這個庫中我將監聽主題更換的類開放了出來,假如當app處於默認主題時我想讓一個按鈕的圖片處於黑色狀態,其餘主題處於白色狀態,可以使用下面的方式app
// 監聽主題的更換,設置本身想要的顏色 ListeningThemeAdjuster.listeningTheme.asObservable().subscribe(onNext: { (colorStr:String) in if colorStr != "#2F4F4F" { self.leftBtn.tintColor = UIColor.white let img = self.leftBtn.image(for: .normal)?.withRenderingMode(.alwaysTemplate) self.leftBtn.setImage(img, for: .normal) } else { self.leftBtn.tintColor = UIColor.black let img = self.leftBtn.image(for: .normal)?.withRenderingMode(.alwaysTemplate) self.leftBtn.setImage(img, for: .normal) } }).addDisposableTo(disposeBag)
5.該庫依賴於rxswift ,截止目前RxSwift暫未更新到swift4.0,若是發現你的項目引入rxswift後報錯,注意檢查rxswift的編譯版本
buildsettings(搜索swift language version) 將編譯版本改成swift3.2,固然,若是之後支持了swift4.0,就能夠忽略這一步了ide