【问题标题】:How do I specify a custom icon for a static UIApplicationShortcutItem in my iOS 9 App?如何在我的 iOS 9 应用程序中为静态 UIApplicationShortcutItem 指定自定义图标?
【发布时间】:2015-12-09 20:12:15
【问题描述】:

我目前正在使用 3D Touch 为我的 iOS 9 应用实现主屏幕快速操作。我有几个使用来自已定义 UIApplicationShortcutIconType 枚举的现有系统图标的操作。

一个例子:

<dict>
    <key>UIApplicationShortcutItemIconType</key>
    <string>UIApplicationShortcutIconTypeSearch</string>
    <key>UIApplicationShortcutItemTitle</key>
    <string>Search for Parking</string>
    <key>UIApplicationShortcutItemType</key>
    <string>SEARCH</string>
</dict>

但是,对于其中一个操作,我想使用自定义图标。我尝试将 UIApplicationShortcutItemIconType 字符串替换为我的图像资产的名称,但这不起作用。

使用 UIApplicationShortcutIcon.iconWithTemplateImageName() 对动态操作很容易做到,但此操作需要是静态的。

【问题讨论】:

    标签: ios ios9 3dtouch uiapplicationshortcutitem


    【解决方案1】:

    不要使用 UIApplicationShortcutItemIconType 键,而是将其替换为 UIApplicationShortcutItemIconFile 键,然后提供图像文件或 ImageAsset 的名称。

    像这样:

    <dict>
        <key>UIApplicationShortcutItemIconFile</key>
        <string>MyCustomImageName</string>
    </dict>
    

    其余的键可以保持原样。

    【讨论】:

    • 我无法让它工作。你有一个工作图像的例子吗?图像是否需要具有特定尺寸?你用的是什么文件名&lt;string&gt;
    • 来自developer.apple.com/library/ios/documentation/General/Reference/… "图标应该是方形、单色和 35x35 磅,如这些模板文件中所示和 iOS 人机界面指南中所述。"
    • 请注意,70x70 也能正常工作,并且在 Retina 显示器上看起来更好。
    • Eddy,注意我说的是点而不是像素。所以这对应于一个 35px x 35px 1x 图像,一个 70px x 70px 2x 图像用于视网膜,以及一个 105px x 105px 3x 图像用于 6+。
    • @ChrisAllwein 我下载了图像图案,并且有不同的尺寸。第一个尺寸为 70 像素 x 70 像素,第二个尺寸为 104 像素 x 104 像素。 104乘104错了吗?
    【解决方案2】:

    使用 UIApplicationShortcutItemIconFile 作为键,图像文件的名称(带或不带文件扩展名)作为字符串。例如:使用名为“lightning.png”的图像,您可以将以下内容添加到 Info.plist...

    <key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>lightning</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Search for Parking</string>
            <key>UIApplicationShortcutItemType</key>
            <string>SEARCH</string>
        </dict>
    </array>
    

    图像可以存储在您的项目树或 Assets.xcassets 中。如果您将图像存储在 Assets.xcassets 中,如果您将图像集命名为与文件名不同的名称,请使用图像集名称。

    您的图片文件必须是 PNG(如果您需要透明)、正方形、单色和 35x35 像素。多色图像本质上是黑色叠加层。

    这是一张符合上述条件的测试图片:

    只需将此图像保存为“lightning.png”,将其拖到您的项目树中,然后在您的 Info.plist 文件中使用上面的代码。

    对于那些不习惯将 Info.plist 作为源代码进行编辑的人,如果您在 Property List 中进行本机操作,以下是上述内容的外观:

    要将这些快捷方式附加到代码中,您可以在 AppDelegate.swift 中执行此操作。添加以下内容:

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    
        if shortcutItem.type == "SEARCH" {
            print("Shortcut item tapped: SEARCH")
            // go to SEARCH view controller
        }
    
    }
    

    值得注意的是,UIApplicationShortcutItemType 的约定并非全部大写(例如“SEARCH”),而是使用您的包标识符作为前缀:

    com.myapps.shortcut-demo.search
    

    【讨论】:

    • 实际上正确的尺寸是104 × 104 for @3x and 70 x 70 for @2x images。
    • 正确的尺寸是35pt = 105 x 105 @3x and 70 x 70 @2x
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2017-07-20
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    相关资源
    最近更新 更多