You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为兼容iOS 15-18的现有应用适配Icon Composer制作的Glass图标并支持Glass替代图标

如何为兼容iOS 15-18的现有应用适配Icon Composer制作的Glass图标并支持Glass替代图标

看起来你踩了直接替换主图标为.icon文件的坑——旧系统不识别这种新格式,所以才会显示异常。要同时兼容两种图标风格,还要支持iOS 26+的Glass替代图标,得用Xcode资产目录的版本变体+针对性的Info.plist配置来实现,以下是分步解决方案:


一、修复多系统主图标兼容问题

你的核心错误是直接把Target的主图标切换成了MyAppIconGlass.icon,旧系统无法解析这种格式,所以才会显示奇怪的图标。正确的做法是用图标集的版本变体让Xcode自动为不同系统分发对应图标:

  1. 恢复原有主图标集
    回到Target的General → App Icons and Launch Screen,把App Icon选项改回你原来的MyAppIcon(也就是旧的.appiconset资产目录)。

  2. 添加iOS 26+的主图标变体

    • 打开Assets.xcassets里的MyAppIcon图标集
    • 右侧属性检查器,找到「App Icon」区域,点击+ Variant,选择iOS 26.0 or later
    • 在这个新变体的「Primary App Icon」下拉菜单中,选择你的MyAppIconGlass.icon文件
    • 确保「Include all app icon assets」选项是开启的(在Target的General设置里)

这样配置后,Xcode会自动在iOS 26+设备上加载Glass主图标,iOS 15-18设备则加载原来的旧风格图标。


二、配置iOS 26+的Glass替代图标

.icon格式的替代图标不能直接通过setAlternateIconName调用,需要和主图标一样,通过图标集变体或Info.plist配置关联:

方法1:用资产目录变体(推荐,无需手动改plist)

  1. 准备Glass替代图标文件
    用Icon Composer创建每个替代图标的.icon文件(比如MyAppIconRedGlass.icon),添加到项目后,确保它被包含在目标的Copy Bundle Resources构建阶段里。

  2. 在主图标集里添加替代图标变体

    • 回到MyAppIcon图标集,右侧属性检查器找到「Alternate App Icons」区域,点击+ Alternate Icon
    • 给这个替代图标命名(比如MyAppIconRedGlass,这个名字就是你要传给setAlternateIconName的字符串)
    • 选中这个新的替代图标,在属性检查器里点击+ Variant,选择iOS 26.0 or later,并关联对应的MyAppIconRedGlass.icon文件
    • 对于旧系统的替代图标(比如MyAppIconRed),确保它的变体设置为iOS 15.0 to 18.99,关联原来的.appiconset资源
  3. 代码中根据系统版本切换图标
    在调用setAlternateIconName时,做版本判断:

    func setAlternateIcon(isRedStyle: Bool) {
        let targetIconName: String?
        if #available(iOS 26.0, *) {
            targetIconName = isRedStyle ? "MyAppIconRedGlass" : nil // nil 恢复主图标
        } else {
            targetIconName = isRedStyle ? "MyAppIconRed" : nil
        }
        
        UIApplication.shared.setAlternateIconName(targetIconName) { error in
            if let error = error {
                print("切换图标失败:\(error.localizedDescription)")
            }
        }
    }
    

方法2:手动配置Info.plist(适合复杂场景)

如果需要更精细的控制,可以手动编辑Info.plist,为不同系统版本指定不同的替代图标:

<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <!-- 旧系统替代图标 -->
        <key>MyAppIconRed</key>
        <dict>
            <key>CFBundleIconAsset</key>
            <string>MyAppIconRed</string>
            <key>CFBundleIconPlatforms</key>
            <array>
                <dict>
                    <key>CFBundleIconPlatformIdentifier</key>
                    <string>com.apple.ios</string>
                    <key>CFBundleIconMinimumVersion</key>
                    <string>15.0</string>
                    <key>CFBundleIconMaximumVersion</key>
                    <string>18.99</string>
                </dict>
            </array>
        </dict>
        <!-- iOS 26+ Glass替代图标 -->
        <key>MyAppIconRedGlass</key>
        <dict>
            <key>CFBundleIconFile</key>
            <string>MyAppIconRedGlass</string>
            <key>CFBundleIconPlatforms</key>
            <array>
                <dict>
                    <key>CFBundleIconPlatformIdentifier</key>
                    <string>com.apple.ios</string>
                    <key>CFBundleIconMinimumVersion</key>
                    <string>26.0</string>
                </dict>
            </array>
        </dict>
    </dict>
    <!-- 主图标版本配置 -->
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconAsset</key>
        <string>MyAppIcon</string>
        <key>CFBundleIconPlatforms</key>
        <array>
            <dict>
                <key>CFBundleIconPlatformIdentifier</key>
                <string>com.apple.ios</string>
                <key>CFBundleIconMinimumVersion</key>
                <string>15.0</string>
                <key>CFBundleIconMaximumVersion</key>
                <string>18.99</string>
            </dict>
            <dict>
                <key>CFBundleIconPlatformIdentifier</key>
                <string>com.apple.ios</string>
                <key>CFBundleIconMinimumVersion</key>
                <string>26.0</string>
                <key>CFBundleIconFile</key>
                <string>MyAppIconGlass</string>
            </dict>
        </array>
    </dict>
</dict>

关键注意事项

  1. 清理构建缓存:每次修改图标配置后,执行Product → Clean Build Folder(快捷键Shift+Command+K),避免Xcode缓存旧资源导致显示异常。
  2. 验证资源包含:所有.icon文件和旧图标集都要确保被添加到目标的Copy Bundle Resources中。
  3. 不要直接用.icon作为主图标:旧系统不支持这种格式,必须通过版本变体做隔离,否则会出现解析错误。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 11:08:00