Swift简单快速的动态更换app图标AppIcon方法示例

来自:网络
时间:2023-07-25
阅读:
目录

关键方法

  • 获取到当前AppIcon的名称
 // 获取到当前AppIcon的名称
let iconName = UIApplication.shared.alternateIconName 
  • 是否支持切换APPIcon图
 // 是否支持切换APPIcon图
if UIApplication.shared.supportsAlternateIcons {
}
  • 切换APPIcon图方法
 // 切换APPIcon图方法 参数填写是AppIcon的名称(Assets 中的图片名称)
        UIApplication.shared.setAlternateIconName("AppIcon 1") { err in
            if err == nil {
                //成功了,跳转到成功报告页
            }
        }

具体代码及UI

页面UI例图

Swift简单快速的动态更换app图标AppIcon方法示例

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 {
                         //成功了,跳转到成功报告页
                    }
                }
            }
        }
    }
}

Assets 图片设置 APPIcon名称与上面的要一致

AppIcon 文件配置

Swift简单快速的动态更换app图标AppIcon方法示例

以上就是Swift简单快速的动态更换app图标AppIcon方法示例的详细内容,更多关于Swift 动态更换app图标-AppIcon的资料请关注其它相关文章!

返回顶部
顶部