前幾天Apple發(fā)布了iOS10.3的更新包屿笼,這個更新包帶來了很多新的特性等舔,特意去研究了一下。
無奈自己的拖延癥糟趾,現(xiàn)在才發(fā)出來慌植。先附上Demo地址。
其實這個已經(jīng)有App第一時間做了這個功能义郑,并且上線了蝶柿,大家可以去AppStore搜索一個App:MLB.com At Bat,觀看一下這個App的介紹視頻非驮,就會知道這個是什么意思了交汤。
這是官方文檔,但是你還需要在 info.plist 里面填一些東西才能讓它起作用劫笙,這部分官方注釋內(nèi)容在這里芙扎。可以查看一下table5填大。
需要在plist文件里面添加如下代碼:
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blackIcon</string>
</array>
</dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>blueIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blueIcon</string>
</array>
</dict>
<key>blackIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blackIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>redIcon</key>
<dict>
<key>UIPrerenderedIcon</key>
<false/>
<key>CFBundleIconFiles</key>
<array>
<string>redIcon</string>
</array>
</dict>
</dict>
</dict>
其中icon要對應你的圖標的名字
其中有一個key UIPrerenderedIcon
這個一開始我也不知道是做什么的戒洼,但是后來看了官方文檔,發(fā)現(xiàn)這個就是在之前的iOS版本用來給圖標添加高光用的允华,其實這個在SB中也有設置的地方
我試了圈浇,不添加這個鍵也不會有任何影響。
好了準備工作做好了就要添加修改圖標的代碼了
- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));```
[[UIApplication sharedApplication] setAlternateIconName:IconName completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"成功更換為%@",IconName);
}else{
NSLog(@"error:%@",error);
}
}];
具體大家可以看我上面的Demo靴寂,非常易懂磷蜀。設置為nil的時候就是還原原來的圖標。
如果大家想看swift版本的和ipad的可以移步百炬。
參考文檔http://www.tuicool.com/articles/miuAfuJ
https://forums.developer.apple.com/thread/71463