Skip to content

Commit 59287ef

Browse files
JmoVxiaJmoVxia
authored andcommitted
增加夜间模式切换
1 parent 38a8eae commit 59287ef

File tree

80 files changed

+4907
-2738
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+4907
-2738
lines changed

CLDemo-Swift/CLDemo-Swift.xcodeproj/project.pbxproj

Lines changed: 127 additions & 15 deletions
Large diffs are not rendered by default.

CLDemo-Swift/CLDemo-Swift/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import UIKit
1111
class AppDelegate: UIResponder, UIApplicationDelegate {
1212
var window: UIWindow?
1313
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
14+
if #available(iOS 13.0, *) {
15+
window?.overrideUserInterfaceStyle = CLTheme.mode.style
16+
}
1417
let tabBarController = CLTabBarController()
1518
window?.rootViewController = tabBarController
1619
window?.makeKeyAndVisible()

CLDemo-Swift/CLDemo-Swift/Assets.xcassets/PopupMeal/Contents.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// CLTheme.swift
3+
// CLDemo-Swift
4+
//
5+
// Created by Chen JmoVxia on 2021/7/15.
6+
//
7+
8+
import UIKit
9+
10+
11+
extension CLTheme {
12+
/// 模式
13+
enum Mode: String, CaseIterable {
14+
///跟随系统
15+
case follow
16+
///白天
17+
case light
18+
///夜间
19+
case dark
20+
21+
@available(iOS 13.0, *)
22+
/// 风格
23+
var style: UIUserInterfaceStyle {
24+
switch self {
25+
case .follow: return .unspecified
26+
case .light: return .light
27+
case .dark: return .dark
28+
}
29+
}
30+
}
31+
}
32+
33+
class CLTheme {
34+
@CLUserDefaultStorage(keyName: "appTheme")
35+
private static var appTheme: String?
36+
37+
///模式
38+
static var mode: Mode {
39+
get { return Mode(rawValue: appTheme ?? "") ?? .follow }
40+
set { appTheme = newValue.rawValue }
41+
}
42+
/// 创造颜色
43+
static func makeColor(light: UIColor, dark: UIColor) -> UIColor {
44+
if #available(iOS 13.0, *) {
45+
return UIColor { $0.userInterfaceStyle == .light ? light : dark }
46+
} else {
47+
return CLTheme.mode == .light ? light : dark
48+
}
49+
}
50+
/// 创造图片
51+
static func makeImage(light: UIImage, dark: UIImage) -> UIImage {
52+
if #available(iOS 13.0, *) {
53+
let image = UIImage()
54+
image.imageAsset?.register(light, with: .init(userInterfaceStyle: .light))
55+
image.imageAsset?.register(dark, with: .init(userInterfaceStyle: .dark))
56+
return image
57+
} else {
58+
return CLTheme.mode == .light ? light : dark
59+
}
60+
}
61+
}
62+
63+
extension UIColor {
64+
static func color(light: UIColor, dark: UIColor) -> UIColor {
65+
return CLTheme.makeColor(light: light, dark: dark)
66+
}
67+
}
68+
69+
extension UIImage {
70+
static func image(light: UIImage, dark: UIImage) -> UIImage {
71+
return CLTheme.makeImage(light: light, dark: dark)
72+
}
73+
}

CLDemo-Swift/CLDemo-Swift/Class/Base/CLController/CLController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CLController: UIViewController {
5454
private lazy var titleLabel: UILabel = {
5555
let view = UILabel()
5656
view.font = PingFangSCBold(18)
57-
view.textColor = .hex("333333")
57+
view.textColor = .color(light: .hex("333333"), dark: .white)
5858
return view
5959
}()
6060
}
@@ -84,7 +84,7 @@ extension CLController {
8484
//MARK: - JmoVxia---布局
8585
private extension CLController {
8686
func initUI() {
87-
view.backgroundColor = .white
87+
view.backgroundColor = .color(light: .white, dark: .hex("#666666"))
8888
navigationItem.titleView = titleLabel
8989
let target = navigationController?.interactivePopGestureRecognizer?.delegate
9090
let pan = UIPanGestureRecognizer.init(target: target, action: Selector(("handleNavigationTransition:")))
@@ -117,7 +117,7 @@ extension CLController {
117117

118118
}
119119
get {
120-
return .light
120+
return CLTheme.mode.style
121121
}
122122
}
123123
}

CLDemo-Swift/CLDemo-Swift/Class/Base/CLNavigationController/CLBackView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CLBackView: UIControl {
2929
}
3030
var themeColor: UIColor = .black {
3131
didSet {
32-
backimageView.image = UIImage(named: "navigationBack")
32+
backimageView.image = .image(light: UIImage(named: "navigationBack")!, dark: (UIImage(named: "navigationBack")?.tintImage(.white))!)
3333
textLabel.textColor = themeColor;
3434
}
3535
}
@@ -41,7 +41,7 @@ class CLBackView: UIControl {
4141
}()
4242
private lazy var backimageView: UIImageView = {
4343
let view = UIImageView()
44-
view.image = UIImage(named: "navigationBack")?.tintImage(themeColor)
44+
view.image = .image(light: UIImage(named: "navigationBack")!, dark: (UIImage(named: "navigationBack")?.tintImage(.white))!)
4545
return view
4646
}()
4747
}

CLDemo-Swift/CLDemo-Swift/Class/CLChangeFontSizeController/CLChangeFontSizeController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension CLChangeFontSizeController {
7171
private extension CLChangeFontSizeController {
7272
func initUI() {
7373
updateTitleLabel { (label) in
74-
label.text = "字体大小"
74+
label.text = "夜间模式".localized
7575
}
7676
view.backgroundColor = UIColor.white
7777
view.addSubview(slider)

0 commit comments

Comments
 (0)