Swift簡單快速的動態(tài)更換app圖標(biāo)AppIcon方法示例
更新時間:2023年06月12日 11:30:54 作者:山水域
這篇文章主要為大家介紹了Swift動態(tài)更換app圖標(biāo)AppIcon的簡單快速方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
關(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 文件配置
以上就是Swift簡單快速的動態(tài)更換app圖標(biāo)AppIcon方法示例的詳細(xì)內(nèi)容,更多關(guān)于Swift 動態(tài)更換app圖標(biāo)-AppIcon的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
RxSwift實現(xiàn)替換delegate的方法示例
這篇文章主要給大家介紹了關(guān)于RxSwift實現(xiàn)替換delegate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用RxSwift具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Swift之UITabBarController 導(dǎo)航控制器的自定義
本文給大家介紹swift導(dǎo)航控制器之UITabBarController,本文通過代碼實例給大家講解swift導(dǎo)航控制器,導(dǎo)航控制器類繼承UITabBarController,代碼簡單易懂,需要的朋友可以參考下2015-10-10