如何将自定义图片作为UIApplicationShortcutItem的UIApplicationShortcutIcon添加?

4

如何将自定义图像添加为 UIApplicationShortcutItemUIApplicationShortcutIcon?这类似于照片应用程序中的最近照片 UIApplicationShortcutItem。因为所有图标都是从自定义图像创建的。提供的图像名称将从应用程序捆绑包中加载,并且将被掩蔽以符合系统定义的图标样式。那么我如何在编程中获取彩色图像作为 UIApplicationShortcutIcon

2个回答

2
你可以从应用程序代理添加快捷图标。这里的 img_editProductimg_Classifieds 是在 .xcassests 文件中添加的自定义图像。
- (void)shortcutsWithIcon
{
    @try 
    {
       UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"img_editProduct"];
       UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"img_Classifieds"];

       UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"com.3dtouchApp.postAnItem" localizedTitle:@"Post an Item" localizedSubtitle:@"Add new product for sale" icon:icon1 userInfo:nil];
       UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"com.3dtouchApp.LatestAds" localizedTitle:@"Latest Ads" localizedSubtitle:@"View top recent Ads" icon:icon2 userInfo:nil];
       NSArray *items = @[item2, item1];

       [UIApplication sharedApplication].shortcutItems = items;
    }
    @catch (NSException *exception) {

    }
}


 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   if (self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
       [self shortcutsWithIcon];
         if ([item.type isEqualToString:@"com.3dtouchApp.postAnItem"])
         {
           ***//Code for launch your screen***
         }

         if ([item.type isEqualToString:@"com.3dtouchApp.LatestAds"])
         {
            ***//code for launch your screen***
         }
    }
return YES;
}

0
请勿使用表情符号代替模板图标。当文本右对齐时,表情符号无法对齐。此外,表情符号是全彩色的,而模板图标应为单色。您可以从许多系统提供的模板图标中选择,也可以创建自定义模板图标。有关图标大小、填充和定位的详细指导,请从https://developer.apple.com/design/downloads/Quick-Action-Guides.zip下载主屏幕快速操作图标模板。如需了解有关设计模板图标的更多信息,请参阅模板图标。 根据以上内容,我认为目前无法显示彩色图标。或许在将来的更新中会有所改进。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接