https://www.jianshu.com/p/6fd1324cfbae
參考Github文獻:https://github.com/jiecao-fm/SwiftTheme/blob/master/README_CN.md
前言
緣起
項目需求,我們要為節操精選開發夜間模式功能。我們的需求不是簡單的調整亮度或者alpha,而是更換為一套更深色的UI。因此所謂夜間模式其實就是特定的更換主題(換膚)功能。
如何實現呢?判斷某個全局變量,然后在初始化視圖控件時設置不同的背景色或者加載不同的切圖文件?但是在切換主題時,已經初始化好的視圖控件呢?沒錯,也許你也想到了通過通知讓相應的視圖控件修改背景色或切圖。想到這里你應該也意識到了Controller中將充斥着注冊通知、if...else、更新視圖控件的代碼,糟糕的是如果忘記了注銷通知還可能引起應用崩潰。
一番思考后,我們對該任務提出了更高的要求,打造一套簡單、可復用的主題框架,正如你看到的這樣。
目標
將SwiftTheme打造為一款簡單、功能豐富、高性能、可擴展的主題框架(換膚框架),為iOS 平台提供一個統一的主題解決方案。
示例
索引方式
讓 UIView 隨主題變換背景色:
view.theme_backgroundColor = ["#FFF", "#000"]
讓 UILabel 和 UIButton 隨主題變換文字顏色:
label.theme_textColor = ["#000", "#FFF"]
button.theme_setTitleColor(["#000", "#FFF"], forState: .Normal)
讓 UIImageView 隨主題變換切圖:
imageView.theme_image = ["day", "night"]
// 不想通過切圖名,想通過 UIImage 來設置不同主題的圖片也是可以的
imageView.theme_image = ThemeImagePicker(images: image1, image2)
然后,當你執行如下代碼時,奇跡發生了!
// 例如isNight為true,imageView將會使用 "night" 的切圖
ThemeManager.setTheme(index: isNight ? 1 : 0)
作者:路漫漫其修遠兮Wzt
鏈接:https://www.jianshu.com/p/6fd1324cfbae
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。
Swift4.x普通筆記06 SwiftTheme主題換膚,Swift廣播的基本使用,X的適配
https://blog.csdn.net/v2810769/article/details/84725480
2018年12月03日 15:09:22 夢中一夜下江南 閱讀數:117
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/v2810769/article/details/84725480
這一節來把SwiftTheme搞明白怎么使用,它是一個主題換膚的框架。下面開始使用
想說一個它的問題,不能跨控制器使用,如果要用的話就要用到廣播。這個我們稍后會講。
X的適配提前講了,812是它的高度
X的底部高度改成49就可以了。貓耳朵改高一些就好了。
首先 pod 'SwiftTheme' 后面不加版本代表使用最新版本
pod update (時間多的話,用這條)
pod update --verbose --no-repo-update (時間不多的話,用這條)
然后command + B 編譯一下
這樣我們就安裝好了
接下來你會想怎么使用,接下來是我實現過的流程。我們寫一個枚舉類MyTheme,你如果用,直接復制。
import Foundation
import SwiftTheme
enum MyTheme:Int {
白天的標識
case day = 0
黑夜的標識
case night = 1
定義之前的標識
static var before = MyTheme.day
定義現在的標識
static var current = MyTheme.night
用於切換主題,主要的類ThemeManager,根據plist來使用
static func switchTo(_ theme:MyTheme){
這邊做一個替換
before = current
current = theme
這邊設置主題
switch theme {
case .day:ThemeManager.setTheme(plistName: "default_theme", path: .mainBundle)
case .night:ThemeManager.setTheme(plistName: "night_theme", path: .mainBundle)
}
}
這邊選擇設置主題
static func switchNight(_ isToNight:Bool) {
switchTo(isToNight ? .night:.day)
}
static func isNight() -> Bool{
return current == .night
}
}
接下來的步驟是定義兩個plist文件,你有幾個主題定義幾個plist文件
--------------------------------------------------------------------------------------------------------------------------------------------
default_theme.plist
圖片分一組
.......
images :
likeButtonbg : "like_btn_24*24"
顏色分一組
........
colors:
black: "#000000"
--------------------------------------------------------------------------------------------------------------------------------------------
night_theme.plist
圖片分一組
.......
images :
likeButtonbg : "like_btn_night_24*24"
顏色分一組
........
colors:
black: "#000000"
--------------------------------------------------------------------------------------------------------------------------------------------
我這里是調用枚舉類的靜態方法,MyTheme.switchNight(true)
到這里我們就完成了7成,剩下的只要像下面這樣調用就好了,如果不懂可以來博客下留言。
leftLabel.theme_textColor = "colors.black"
rightLabel.theme_textColor = "colors.cellRightTextColor"
rightImageView.theme_image = "images.cellRightArrow"
separatorView.theme_backgroundColor = "colors.separatorColor"
theme_backgroundColor = "colors.cellBackgroundColor"
.....這里換成你對應的控件.....
leftLabel.theme_textColor = "colors.black"
rightLabel.theme_textColor = "colors.cellRightTextColor"
rightImageView.theme_image = "images.cellRightArrow"
theme_backgroundColor = "colors.cellBackgroundColor"
topView.theme_backgroundColor = "colors.cellBackgroundColor"
collectionView.theme_backgroundColor = "colors.cellBackgroundColor"
...................
這樣我們的換膚就好了,我利用的是按鈕的點擊,選中為夜晚,不選中為白天。
接下來,我們實現一個功能,就是當我關閉App時,再次打開時,還是會顯示上次保存的主題。
在我們的按鈕的點擊事件里面,實現狀態的保存。以下是代碼。
@IBAction func dayOrNightButtonClicked(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
這一行,就可以實現狀態的保存
UserDefaults.standard.set(sender.isSelected, forKey: "isNight")
}
讀取狀態,然后進行設置。我在AppDelegate.swift進行設置。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//根據isNight,讀取主題
ThemeManager.setTheme(plistName: UserDefaults.standard.bool(forKey: isNight) ? "night_theme":"default_theme", path: .mainBundle)
.....................
}
接下來是控制器之間的通知使用方法
1,在我們切換主題的按鈕里面使用NotificationCenter,,,發送
@IBAction func dayOrNightButtonClicked(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
.................
這里的dayOrNightButtonCLicked只是作為標識,
sender.isSelected這是要傳遞過去的變量。
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dayOrNightButtonCLicked"), object: sender.isSelected)
}
2,,,在目錄控制器里面接收
override func viewDidLoad() {
super.viewDidLoad()
..............
這個是回調的方法。
receiveDayOrNightButtonClicked
NotificationCenter.default.addObserver(self, selector: #selector(receiveDayOrNightButtonClicked), name: NSNotification.Name(rawValue: "dayOrNightButtonCLicked"), object: nil)
}
下面我們來定義這個receiveDayOrNightButtonClicked
@objc func receiveDayOrNightButtonClicked(notification:Notification) {
這里我們得到了我們的對象
let selected = notification.object as! Bool
...........接下類就是你的邏輯了
}
記住通知用完要進行銷毀,如果在控制器里面不銷毀的話,可能會崩潰。
deinit {
銷毀通知
NotificationCenter.default.removeObserver(self)
}
沒講太多東西,就兩個,SwiftTheme主題換膚,Swift廣播的基本使用。