在上一篇博客里面写了oc修改图片的方法。由于plist文件的设置方法的都是一样的,我就不多加赘述了。直接上swift的代码
首先在plist文件里配置Icon Files
在需要调用更换图标的方法里调用如下函数
func changeAppIconWithName(iconName:String) {
//10.3以上可用
var icon = iconName
if !UIApplication.shared.supportsAlternateIcons {
return;
}
if icon == "" {
icon = ""
}
UIApplication.shared.setAlternateIconName(iconName) { (error) in
if (error != nil) {
print("更换图标发生错误:\(String(describing: error))")
}
}
}
如果不想出现弹框,同样可以使用runtime的method替换,代码如下
extension SchoolController {
public class func initializeMethod(){
let presentM = class_getInstanceMethod(self, #selector(present(_:animated:completion:)))
let presenrSwizzlingM = class_getInstanceMethod(self, #selector(lq_presentViewController(viewControllerToPresent:animted:completion:)))
method_exchangeImplementations(presentM!, presenrSwizzlingM!)
}
@objc func lq_presentViewController(viewControllerToPresent:UIViewController, animted:Bool, completion:()->Void) {
if viewControllerToPresent .isKind(of: UIAlertController.self) {
let alertContrller:UIAlertController = viewControllerToPresent as! UIAlertController
if alertContrller.title == nil && alertContrller.message == nil {
return
}
}
self.lq_presentViewController(viewControllerToPresent: viewControllerToPresent, animted: animted, completion: completion)
}
需要注意的是这个扩展需要调用,单独调用,我是在这里调用的,只要在调用更换图标的方法之前调用应该都可以
override func viewWillAppear(_ animated: Bool) {
SchoolController.initializeMethod()
}
}