iOS/macOS-绘制图像/图标以供系统着色的最佳方式
创建一组受操作系统影响的图标以显示彩色/重音版本的最佳方法是什么 就如,;iOS/macOS-绘制图像/图标以供系统着色的最佳方式,ios,swift,macos,icons,Ios,Swift,Macos,Icons,创建一组受操作系统影响的图标以显示彩色/重音版本的最佳方法是什么 就如,; 选定版本 重音版本 我有大约10个黑色版本,但想有选择或系统选择的口音颜色调整他们。我应该如何处理我的图标,让它们被操作系统着色 有路线图吗 这段代码向您展示了如何根据iOS主题和设置包添加并设置一个可选图标。我还添加了accent1Icon和accent2Icon作为示例 在Settings.bundle中,您需要将多值应用程序项添加到首选项中(苹果要求用户可以更改主题,即使您根据其iOS设置自动设置主题): 将
- 选定版本
- 重音版本
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>darkIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-dark-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>accent1Icon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-accent1-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>accent2Icon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-accent2-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
您可以在AppDelegate应用程序IDBECOMEACTIVE(或其他地方)中运行changeIcon代码。如果选择在那里运行,则需要将其添加到AppDelegate文件中:
enum AppIconTheme: String {
case UseiOSTheme = "0"
case DarkTheme = "1"
case LightTheme = "2"
case Accent1Theme = "3"
case Accent2Theme = "4"
}
func applicationDidBecomeActive(_ application: UIApplication) {
// update icon if dark mode
if #available(iOS 12.0, *) {
var theme = AppIconTheme.UseiOSTheme
if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
if let themeSettings = AppIconTheme(rawValue: iconTheme) {
theme = themeSettings
}
}
print(theme as Any)
switch (theme) {
case .UseiOSTheme:
if UIApplication.shared.windows[0].rootViewController?.traitCollection.userInterfaceStyle == .dark {
self.changeIcon(to: "darkIcon")
} else {
self.changeIcon(to: nil)
}
case .LightTheme:
self.changeIcon(to: nil)
case .DarkTheme:
self.changeIcon(to: "darkIcon")
case .Accent1Theme:
self.changeIcon(to: "accent1Icon")
case .Accent2Theme:
self.changeIcon(to: "accent2Icon")
}
} else {
// Fallback on earlier versions
var theme = AppIconTheme.UseiOSTheme
if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
theme = AppIconTheme(rawValue: iconTheme)!
}
print(theme as Any)
switch (theme) {
case .UseiOSTheme:
self.changeIcon(to: nil)
case .LightTheme:
self.changeIcon(to: nil)
case .DarkTheme:
self.changeIcon(to: "darkIcon")
case .Accent1Theme:
self.changeIcon(to: "accent1Icon")
case .Accent2Theme:
self.changeIcon(to: "accent2Icon")
}
}
}
func changeIcon(to name: String?) {
//Check if the app supports alternating icons
guard UIApplication.shared.supportsAlternateIcons else {
return;
}
if UIApplication.shared.alternateIconName != name {
//Change the icon to a specific image with given name
// if name is nil, the app icon will be the default app icon
UIApplication.shared.setAlternateIconName(name) { (error) in
//After app icon changed, print our error or success message
if let error = error {
print("App icon failed to due to \(error.localizedDescription)")
} else {
print("App icon changed successfully.")
}
}
}
}
以下是Apple在setAlternativeIcon上提供的文档:
非常感谢你。我会试试看。再次感谢您。@LordAnubis,很乐意帮忙@洛丹努比斯,如果这个答案解决了你的问题,请接受它。:-)我会忙着在macOS项目中实现它。
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>darkIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-dark-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>accent1Icon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-accent1-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>accent2Icon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>name-of-accent2-icon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
YourProject
|
+-- Alternate Icons (folder)
|
+-- name-of-dark-icon@2x.png
+-- name-of-dark-icon@3x.png
+-- name-of-accent1-icon@2x.png
+-- name-of-accent1-icon@3x.png
+-- name-of-accent2-icon@2x.png
+-- name-of-accent2-icon@3x.png
enum AppIconTheme: String {
case UseiOSTheme = "0"
case DarkTheme = "1"
case LightTheme = "2"
case Accent1Theme = "3"
case Accent2Theme = "4"
}
func applicationDidBecomeActive(_ application: UIApplication) {
// update icon if dark mode
if #available(iOS 12.0, *) {
var theme = AppIconTheme.UseiOSTheme
if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
if let themeSettings = AppIconTheme(rawValue: iconTheme) {
theme = themeSettings
}
}
print(theme as Any)
switch (theme) {
case .UseiOSTheme:
if UIApplication.shared.windows[0].rootViewController?.traitCollection.userInterfaceStyle == .dark {
self.changeIcon(to: "darkIcon")
} else {
self.changeIcon(to: nil)
}
case .LightTheme:
self.changeIcon(to: nil)
case .DarkTheme:
self.changeIcon(to: "darkIcon")
case .Accent1Theme:
self.changeIcon(to: "accent1Icon")
case .Accent2Theme:
self.changeIcon(to: "accent2Icon")
}
} else {
// Fallback on earlier versions
var theme = AppIconTheme.UseiOSTheme
if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
theme = AppIconTheme(rawValue: iconTheme)!
}
print(theme as Any)
switch (theme) {
case .UseiOSTheme:
self.changeIcon(to: nil)
case .LightTheme:
self.changeIcon(to: nil)
case .DarkTheme:
self.changeIcon(to: "darkIcon")
case .Accent1Theme:
self.changeIcon(to: "accent1Icon")
case .Accent2Theme:
self.changeIcon(to: "accent2Icon")
}
}
}
func changeIcon(to name: String?) {
//Check if the app supports alternating icons
guard UIApplication.shared.supportsAlternateIcons else {
return;
}
if UIApplication.shared.alternateIconName != name {
//Change the icon to a specific image with given name
// if name is nil, the app icon will be the default app icon
UIApplication.shared.setAlternateIconName(name) { (error) in
//After app icon changed, print our error or success message
if let error = error {
print("App icon failed to due to \(error.localizedDescription)")
} else {
print("App icon changed successfully.")
}
}
}
}