Swift 動態(tài)更換app圖標(biāo)-AppIcon(簡單快速)

關(guān)鍵方法

  • 獲取到當(dāng)前AppIcon的名稱
 // 獲取到當(dāng)前AppIcon的名稱
let iconName = UIApplication.shared.alternateIconName 
  • 是否支持切換APPIcon圖
 // 是否支持切換APPIcon圖
if UIApplication.shared.supportsAlternateIcons {

}
  • 切換APPIcon圖方法
 // 切換APPIcon圖方法 參數(shù)填寫是AppIcon的名稱(Assets 中的圖片名稱)
        UIApplication.shared.setAlternateIconName("AppIcon 1") { err in
            if err == nil {
                //成功了,跳轉(zhuǎn)到成功報告頁
            }
        }

具體代碼及UI

頁面UI例圖


class MCAppIconVC: UIViewController {

    var selectInt = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 獲取到當(dāng)前AppIcon的名稱
        let iconName = UIApplication.shared.alternateIconName 
        if let tagStr = iconName?.replacingOccurrences(of: "AppIcon ", with: "") {// 因為我設(shè)置的APPIcon名稱是"AppIcon 1", "AppIcon 2"等 這樣可以獲取后面的數(shù)字 布局UI
            let tag = Int(tagStr) ?? 1
            for i in 11...14 {
                if let imageView = self.view.viewWithTag(i) as? UIImageView {// 所有按鈕設(shè)置未未選中
                    imageView.image = UIImage(named: "btn_normal_icon")
                }
            }
            if let imageView = self.view.viewWithTag(10+tag) as? UIImageView {// 設(shè)置選中的UI
                imageView.image = UIImage(named: "btn_npresses_icon")
            }
            selectInt = tag // 記錄當(dāng)前選中的是哪個
        }
        // 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 {
                        //成功了,跳轉(zhuǎn)到成功報告頁
                    }
                }
            }else if UIApplication.shared.alternateIconName == nil, selectInt != 1 {
                UIApplication.shared.setAlternateIconName("AppIcon \(selectInt)") { err in
                    if let err = err {
                        
                    }else {
                         //成功了,跳轉(zhuǎn)到成功報告頁
                    }
                }
            }
        }
    }

}

Assets 圖片設(shè)置 APPIcon名稱與上面的要一致

AppIcon 文件配置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容