是否可以将图像添加到UIActionSheet
如中所示的按钮UIDocumentInteractionController
?如果是这样,请让我知道它是如何完成的。
12 回答
可以将图像(确切地说:图标或符号)添加到 a UIActionSheet
(或 a UIAlertView
)的按钮,而无需加载图像文件或摆弄(子)视图。在这些类中,按钮由它们的标题指定,它们是字符串。所以很明显使用符号,也可以通过字符串指定。我想出的第一个是使用unicode 符号。
然后我发现其中一些在 iOS 上被渲染为漂亮的图标,并且在 Mac OS 上的字符查看器中也可以看到几个符号。因此,这些符号实际上可以在可以指定字符串的任何地方使用。
这种方法的缺点是:
- 您仅限于预定义的符号。
- 并非所有符号都按应有的方式呈现(例如
\u29C9
)。 - 在不同的 iOS 版本(例如
\U0001F533
iOS 5 和 6)上,某些符号的外观可能会有所变化。
以下是一些有趣的符号:
如果您想快速检查符号的外观(至少在 Mac OS 上),您可以使用 Calculator。在模拟器中检查:例如\u2B1C
在 Calculator 10.7.1 中不是图标。
截图:
UIActionSheet
按钮标题:
@"\U0001F6A9 \U0001F4CC \u26F3 \u2690 \u2691 \u274F \u25A4 Test"
@"\U0001F4D6 \U0001F30E \U0001F30F \u25A6 \U0001F3C1 \U0001F332 \U0001F333 \U0001F334 Test"
UIAlertView
按钮标题:
@"\u26A0 Yes"
UITableViewCell
带有复选框和其他图标
试试这个方法,希望对你有帮助。
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];
标准 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
}
我不确定图像是否会响应触摸,但您可以构建一个UIActionSheet
类似的UIDocumentInteractionController
.
- (IBAction)actionSheetButtonPressed:(id)sender {
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"Share "
message:@"Select your current status"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* online = [UIAlertAction
actionWithTitle:@"Facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* offline = [UIAlertAction
actionWithTitle:@"Google+"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* doNotDistrbe = [UIAlertAction
actionWithTitle:@"LinkedIn"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* away = [UIAlertAction
actionWithTitle:@"Twitter"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[online setValue:[[UIImage imageNamed:@"facebook.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[offline setValue:[[UIImage imageNamed:@"google-plus.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[doNotDistrbe setValue:[[UIImage imageNamed:@"linkedin.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[away setValue:[[UIImage imageNamed:@"twitter.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[view addAction:online];
[view addAction:away];
[view addAction:offline];
[view addAction:doNotDistrbe];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];
}
您可以通过键“ _buttons ”从 actionSheet 对象获取操作按钮标题并设置按钮图像。
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter", @"Google +", @"E - mail", @"Send Message",nil];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"fb_icon1.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"tweet_icon1.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"googleplus_icon1.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"mail_icon.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:4] setImage:[UIImage imageNamed:@"message_icon.png"] forState:UIControlStateNormal];
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
}
}
[actionSheet showInView:self.view];
对于 iOS 8,请参考这里
if( [UIAlertController class] ){
UIAlertController *view = [UIAlertController alertControllerWithTitle:@"Main Title"
message:@"What do you want to do?"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *firstAA = [UIAlertAction actionWithTitle:@"Beep Beep"
style:UIAlertActionStyleDefault
handler:^( UIAlertAction *action ){
[view dismissViewControllerAnimated:YES
completion:nil];
}];
[firstAA setValue:[UIImage imageNamed:@"your-icon-name"] forKey:@"image"];
[view addAction:firstAA];
UIAlertAction *cancelAA = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^( UIAlertAction *action ){
[self deselectTableViewRow];
[view dismissViewControllerAnimated:YES
completion:nil];
}];
[view addAction:cancelAA];
[self presentViewController:view
animated:YES
completion:nil];
}
else {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"What do you want to do?"
delegate:(id)self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[sheet addButtonWithTitle:@"title"];
[[[sheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"your-icon-name.png"] forState:UIControlStateNormal];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];
[sheet showInView:self.view];
}
我刚刚创建了一个类来模拟 UIActionSheet 的外观,它使用支持每一行的图像和文本的表格单元格。它还使用块进行交互,支持 iPhone 和 iPad,从 iPad 上的 UITabBarItem 弹出和多个工作表的排队。仍在开发中,但可以随意从 Github 克隆它:
http://github.com/azplanlos/SIActionSheet
用法很简单,举个例子:
SIActionSheet* mySheet = [SIActionSheet actionSheetWithTitle:@"Action Sheet title"
andObjects:[NSArray arrayWithObjects:
[SIActionElement actionWithTitle:@"Item 1"
image:[UIImage imageNamed:@"image"]
andAction:^{NSLog(@"action 1");}]
, nil]
completition:^(int num) {
NSLog(@"pressed %i", num);
} cancel:^{NSLog(@"canceled");}];
mySheet.followUpSheet = anotherSheet;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[mySheet show];
else
[mySheet showFromTabBarItem:item inTabBar:tabBar];
如果您遇到任何问题,请告诉我。我希望这可以帮助很多像我一样遇到同样问题的人......
我知道答案很晚,但我找到了另一种在操作表中显示图像的方法:
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image:" delegate:self cancelButtonTitle:@"Cancel"destructiveButtonTitle:nil otherButtonTitles: @"Image1", @"Image2", @"Image3", @"Image4", @"Image5", @"Image6", @"Image7", @"Image8",@"Image9", @"Image10", @"Image11", @"Image12", @"Image13", @"Image14", @"Image15", nil];
self.actionSheet.tag = 1;
for (id button in [self.actionSheet valueForKey:@"_buttons"])
{
UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[button titleForState:UIControlStateNormal]]];
[buttonImage setFrame:CGRectMake(5, 5,35,35)];
[button addSubview:buttonImage];
}
[self.actionSheet showInView:[UIApplication sharedApplication].keyWindow];
我发现这个类别扩展在 ios7.1 中可以将图像/图标添加到 UIActionSheet 中的按钮,但有一些警告......
@interface UIActionSheet (GSBActionSheetButtons)
- (void)buttonAtIndex:(NSUInteger)index setImage:(UIImage *)image forState:(UIControlState)state;
@end
@implementation UIActionSheet (GSBActionSheetButtons)
- (void)buttonAtIndex:(NSUInteger)index setImage:(UIImage *)image forState:(UIControlState)state
{
for (UIView* view in self.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
if (index-- == 0) {
UIButton *button = (UIButton*)view;
[button setImage:image forState:state];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageEdgeInsets = UIEdgeInsetsMake(2,0,2,0);
break;
}
}
}
}
并使用它:
[self.sharePopup buttonAtIndex:2 setImage:[UIImage imageNamed:@"twitter.png"] forState:UIControlStateNormal];
警告:
尽管 UIActionSheet 确实正确地将您的图像自动调整为按钮的正确高度,但它似乎并没有相应地改变 imageview宽度; 因此需要 UIViewContentModeScaleAspectFit 来防止图像被压扁。然而,imageview 框架的宽度仍然是原始的全尺寸,所以如果你的图像很大(或更准确地说是宽),那么你会在居中(缩小)图像和按钮文本之间得到一个恼人的间隙。我没有办法解决这个问题;甚至以编程方式向图像视图添加显式宽度=高度约束似乎被忽略了!?[有任何想法吗?]。最终结果,请确保您的图像开始时的高度正确(例如,在 iPhone 4S 上约为 45 像素),否则按钮图像和文本之间的差距会越来越大。
更严重的是,一旦您向按钮添加图像,UIActionSheet 似乎会自动使按钮的文本变为粗体(!)。我不知道为什么也不知道如何防止这种情况[任何想法?]
最后,此解决方案依赖于 UIActionSheet 的子视图与按钮的索引顺序相同。这对于少数按钮来说是正确的,但是(显然)当你的 UIActionSheet 中有很多项目时,Apple 对索引感到困惑[但无论如何你都会在 actionSheet:clickedButtonAtIndex: 中遇到问题:当你试图弄清楚点击了哪个按钮...]
哦,imageEdgeInsets: 是可选的 - 我在按钮内将每个图像插入几个像素,这样图像就不会垂直相互接触。
[意见:鉴于上述奇怪之处,我觉得苹果真的不希望人们在他们的行动表上胡闹。在某些时候,您可能不得不硬着头皮实现自己的模式弹出窗口;这些 UIActionSheets 只能容纳这么多的粗暴处理......]
NSString* strUrl=[MLControl shared].currentServerUrl;
for( MLServerUrl *title in [MLControl shared].arrServerUrl) {
NSString* strShow=title.name;
if ([strUrl isEqualToString: title.url]) {
strShow=[NSString stringWithFormat:@"√ %@",strShow];
}else{
strShow=[NSString stringWithFormat:@" %@",strShow];
}
[chooseImageSheet addButtonWithTitle:strShow];
}
// [[[chooseImageSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"ic_check_black_18dp.png"] forState:UIControlStateNormal];
chooseImageSheet.actionSheetStyle = UIActionSheetStyleDefault;
[chooseImageSheet showFromRect:btnRc inView:sender animated:YES];
从 iOS 8.0 开始,您可以使用UIAlertController
. 在UIAlertController
中,每个按钮项都知道UIAlertAction
相应的添加。