代码中更改Icon图标---iOS笔记摘录

作者: 平安喜乐698 | 来源:发表于2017-09-07 09:26 被阅读11269次
    1. Info.plist
    <key>CFBundleIcons</key>
    <dict>
       <key>CFBundlePrimaryIcon</key>
       <dict>
           <key>CFBundleIconFiles</key>
           <array>
               <string>原AppIcon图标名</string>
           </array>
       </dict>
       <key>CFBundleAlternateIcons</key>
       <dict>
           <key>icon01</key>
           <dict>
               <key>CFBundleIconFiles</key>
               <array>
                   <string>AppIcon图标名2</string>
               </array>
           </dict>
           <key>icon02</key>
           <dict>
               <key>CFBundleIconFiles</key>
               <array>
                   <string>AppIcon图标名3</string>
               </array>
           </dict>
       </dict>
    </dict>
    
    1. 将所有AppIcon放入工程中(不要放在Assets.xcassets)

    2. 代码

    // 更换
    if ([UIApplication sharedApplication].supportsAlternateIcons) {
           [[UIApplication sharedApplication] setAlternateIconName:图标名 completionHandler:^(NSError * _Nullable error) {
               if (!error) {
                   NSLog(@"success:%@",图标名);
               }else{
                   NSLog(@"error:%@",error);
               }
           }];
       } else {
           NSLog(@"不能更换icon");
           return;
       }
    }
    
    // 还原
    if ([UIApplication sharedApplication].alternateIconName != nil) {  // 图标被替换过
           // 重置icon
           [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
               if (!error) {
                   NSLog(@"success");
               } else {
                   NSLog(@"error:%@",error);
               }
           }];
       }
    
    
    

    相关文章

      网友评论

        本文标题:代码中更改Icon图标---iOS笔记摘录

        本文链接:https://www.haomeiwen.com/subject/blibjxtx.html