像在UIDocumentInteractionController中一样将图像添加到UIActionSheet按钮

Ahm*_*ali 33 iphone objective-c uiimage uiactionsheet uidocumentinteraction

是否可以将图像添加到UIActionSheet所见的按钮UIDocumentInteractionController?如果是这样,请告诉我它是如何完成的.

Edd*_*ian 71

有可能将图像(确切地说:图标或符号)添加到UIActionSheet(或a UIAlertView)的按钮而不加载图像文件或摆弄(子)视图.在这些类中,按钮由其标题指定,标题是字符串.因此很明显使用符号,也可以通过字符串指定符号.我想出的第一个是使用unicode符号.

然后我发现它们中的一些在iOS上被渲染为漂亮的图标,并且在Mac OS 的角色查看器中也可以看到几个符号.因此,实际上可以在指定字符串的任何地方使用符号.

这种方法的缺点是:

  • 您仅限于预定义符号.
  • 并非所有符号都按其应有的方式呈现(例如\u29C9).
  • 不同iOS版本上的某些符号的外观可能会发生变化(例如,\U0001F533在iOS 5和6上).

以下是一些有趣的符号:

如果要快速检查符号的外观(至少在Mac OS上),可以使用计算器.在模拟器中明确检查:例如,\u2B1C它不是Calculator 10.7.1中的图标.

截图:

UIActionSheet

UIActionSheet

按钮标题:

@"\U0001F6A9 \U0001F4CC \u26F3 \u2690 \u2691 \u274F \u25A4 Test"
@"\U0001F4D6 \U0001F30E \U0001F30F \u25A6 \U0001F3C1 \U0001F332 \U0001F333 \U0001F334 Test"
Run Code Online (Sandbox Code Playgroud)

UIAlertView

在此输入图像描述

按钮标题:

@"\u26A0 Yes"
Run Code Online (Sandbox Code Playgroud)

UITableViewCell 带有复选框和其他图标

的UITableViewCell


Ami*_*mit 27

试试这种方式,我希望它可以帮到你.

UIActionSheet * action = [[UIActionSheet alloc] 
                      initWithTitle:@"Title" 
                      delegate:self 
                      cancelButtonTitle:@"Cancel" 
                      destructiveButtonTitle:nil 
                      otherButtonTitles:@"",nil];

[[[action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];

[[[action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"yourImage_Highlighted.png"] forState:UIControlStateHighlighted];
Run Code Online (Sandbox Code Playgroud)

  • 它被接受了,但现在它在iOS8中崩溃了 (8认同)
  • [Apple认为](http://stackoverflow.com/a/9054299/957768)访问其视图层次结构中未公开记录的部分为"私有API"; 这种方法会使App Store遭到拒绝. (6认同)

aud*_*nce 6

标准UIActionSheet不支持图像.

添加图像的一种方法UIActionSheet是向子视图添加子视图UIActionSheet.只需实现UIActionSheetDelegate方法willPresentActionSheet:像这样:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
     UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picturename.png"]];
     // Set the frame of the ImageView that it's over the button.
     [actionSheet addSubview:buttonImage];
     [buttonImage release]; // only if you don't need this anymore
}
Run Code Online (Sandbox Code Playgroud)

我不确定图像是否响应触摸,但你可以建立UIActionSheetUIDocumentInteractionController.


Raw*_*ean 5

这是一种方法:https: //github.com/levey/LeveyPopListView 在此输入图像描述

  • 看起来像Android.: - / (7认同)