我在iTunes上看到了这个应用程序,它可以在iPhone上创建自定义图标。在我的应用程序中,我也想更改图标,具体来说,我想做的是在我的图标中有一个标签,我想通过编程方式更改标签的值。
我在iTunes上看到了这个应用程序,它可以在iPhone上创建自定义图标。在我的应用程序中,我也想更改图标,具体来说,我想做的是在我的图标中有一个标签,我想通过编程方式更改标签的值。
从iOS 10.3开始,可以更改应用程序图标。
Swift 3:
if UIApplication.shared.supportsAlternateIcons{
UIApplication.shared.setAlternateIconName("icon2", completionHandler: { (error) in
print(error ?? "")
})
}
Objective C:
[[UIApplication sharedApplication] setAlternateIconName:@"icon2" completionHandler:^(NSError * _Nullable error) {
//NSLog(@"Error...");
}];
//Info.plist
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>Icon1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>icon1</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>Icon2</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>icon2</string>
</array>
</dict>
</dict>
</dict>
参考资料:
只是我的个人看法。
直接添加到plist文件中也没有问题,但是在Xcode中可视化查看时会得到一个“奇怪”的值(IOS5 ...):
2) 在模拟器上(Xcode 10 beta...),在运行时调试控制台中,您将看到:
MobileGestalt.c:890: MGIsDeviceOneOfType在此平台上不受支持。
但是它可以正常工作
3) 不要直接在AppDelegate中调用。如果需要,请以分派方式调用:
final func changeIcon(){
let name = "Icon1"
let icon = UIImage(named: name)
if UIApplication.shared.supportsAlternateIcons{
UIApplication.shared.setAlternateIconName(name, completionHandler: { (error) in
print(error ?? "ok")
})
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline: when) {
self.changeIcon()
}
return true
}
.....
4) 注意:图标名称是您在上一级键中放置的符号名称,例如:
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>Icon1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>logo2_120x120</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>Icon2</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>logo3_120x120</string>
</array>
</dict>
</dict>
</dict>
不要在plist中添加@2x或类似的内容。
这是不可能的。除非您的应用程序属于新闻杂志类别。对于新闻杂志应用程序,请使用代码更改图标。
UIApplication *app = [UIApplication sharedApplication];
[app setNewsstandIconImage:newsstandImage];
注意:@Enrico建议的是另一种解决方案。您的应用程序图标仍将出现在主屏幕上,会创建一个重复的图标。这不是大多数用户喜欢的。