Swift简单快速的动态更换app图标AppIcon方法示例
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>关键方法</li><li>具体代码及UI</li><li>Assets 图片设置 APPIcon名称与上面的要一致</li></ul></div><p class="maodian"></p><h2>关键方法</h2><ul><li>获取到当前AppIcon的名称</li></ul>
<div class="jb51code"><pre class="brush:java;"> // 获取到当前AppIcon的名称
let iconName = UIApplication.shared.alternateIconName
</pre></div>
<ul><li>是否支持切换APPIcon图</li></ul>
<div class="jb51code"><pre class="brush:java;"> // 是否支持切换APPIcon图
if UIApplication.shared.supportsAlternateIcons {
}
</pre></div>
<ul><li>切换APPIcon图方法</li></ul>
<div class="jb51code"><pre class="brush:java;"> // 切换APPIcon图方法 参数填写是AppIcon的名称(Assets 中的图片名称)
UIApplication.shared.setAlternateIconName("AppIcon 1") { err in
if err == nil {
//成功了,跳转到成功报告页
}
}
</pre></div>
<p class="maodian"></p><h2>具体代码及UI</h2>
<p>页面UI例图</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202306/20230612085252014.png" /></p>
<div class="jb51code"><pre class="brush:java;">class MCAppIconVC: UIViewController {
var selectInt = 1
override func viewDidLoad() {
super.viewDidLoad()
// 获取到当前AppIcon的名称
let iconName = UIApplication.shared.alternateIconName
if let tagStr = iconName?.replacingOccurrences(of: "AppIcon ", with: "") {// 因为我设置的APPIcon名称是"AppIcon 1", "AppIcon 2"等 这样可以获取后面的数字 布局UI
let tag = Int(tagStr) ?? 1
for i in 11...14 {
if let imageView = self.view.viewWithTag(i) as? UIImageView {// 所有按钮设置未未选中
imageView.image = UIImage(named: "btn_normal_icon")
}
}
if let imageView = self.view.viewWithTag(10+tag) as? UIImageView {// 设置选中的UI
imageView.image = UIImage(named: "btn_npresses_icon")
}
selectInt = tag // 记录当前选中的是哪个
}
// Do any additional setup after loading the view.
}
// @IBAction func tapGesture(_ sender: UITapGestureRecognizer) {
// let tag = sender.view?.tag ?? 1
// for i in 11...14 {
// if let imageView = self.view.viewWithTag(i) as? UIImageView {
// imageView.image = UIImage(named: "btn_normal_icon")
// }
// if let view = self.view.viewWithTag(i-10) {
// view.layer.cornerRadius = 16
// view.layer.masksToBounds = true
// view.layer.borderColor = UIColor.white.cgColor
// view.layer.borderWidth = 2
// }
// }
// if let imageView = self.view.viewWithTag(10+tag) as? UIImageView {
// imageView.image = UIImage(named: "btn_npresses_icon")
// }
// if let view = self.view.viewWithTag(tag) {
// view.layer.cornerRadius = 16
// view.layer.masksToBounds = true
// view.layer.borderColor = UIColor(named: "ThemeYellow")?.cgColor
// view.layer.borderWidth = 2
// }
// selectInt = tag
// }
@IBAction func setupBtnAction(_ sender: UIButton) { //点击下面的按钮
if UIApplication.shared.supportsAlternateIcons { //是否支持切换APPIcon图
if let iconName = UIApplication.shared.alternateIconName, iconName != "AppIcon \(selectInt)" {
UIApplication.shared.setAlternateIconName("AppIcon \(selectInt)") { err in
if let err = err {
}else {
//成功了,跳转到成功报告页
}
}
}else if UIApplication.shared.alternateIconName == nil, selectInt != 1 {
UIApplication.shared.setAlternateIconName("AppIcon \(selectInt)") { err in
if let err = err {
}else {
//成功了,跳转到成功报告页
}
}
}
}
}
}</pre></div>
<p class="maodian"></p><h2>Assets 图片设置 APPIcon名称与上面的要一致</h2>
<p>AppIcon 文件配置</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202306/20230612085252015.png" /></p>
<p>以上就是Swift简单快速的动态更换app图标AppIcon方法示例的详细内容,更多关于Swift 动态更换app图标-AppIcon的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Swift 并发修改Sendable 闭包实例详解</li><li>Swift之for循环的基础使用学习</li><li>Swift自动调整视图布局AutoLayout和AutoresizingMask功能详解</li><li>Swift 中 Opaque Types学习指南</li><li>Swift 中的 RegexBuilder学习指南</li><li>Swift中的高阶函数功能作用示例详解</li><li>Swift并发系统并行运行多个任务使用详解</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]