回答

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]; 
+1

你是否肯定这种方法是合法的,我可以将我的应用程序发布到App Store? – 2011-05-29 13:10:18

+0

我有同样的问题吗?使用此代码提交给应用商店? – 2011-09-20 16:53:04

+1

那么,这会被拒绝吗? – 2012-07-01 22:21:26

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 
} 

我不知道如果图像响应触摸,但你可以建立一个UIActionSheetUIDocumentInteractionController

71

有增加图象的可能性(准确地说:图标或符号),无负载的图像文件或(子)的观点周围摆弄一个UIActionSheet(或UIAlertView)的按钮。在这些类中,按钮由其标题指定,即字符串。所以使用符号是显而易见的,也可以通过字符串指定。第一个我想出的是使用unicode symbols

然后我发现它们中的一些在iOS上呈现为漂亮的图标,并且可以在Mac OS上的Character Viewer中看到几个符号。因此,符号实际上可以在任何可以指定字符串的地方使用。

这种方法的缺点是:

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

以下是在其他一些有趣的符号:

如果你想快速检查符号的外观像(至少在Mac OS上),你可以使用use the Calculator。请在模拟器中确认:例如\u2B1C不是计算器10.7.1中的图标。

截图:

UIActionSheet

UIActionSheet

按钮标题:

@"\U0001F6A9 \U0001F4CC \u26F3 \u2690 \u2691 \u274F \u25A4 Test" 
@"\U0001F4D6 \U0001F30E \U0001F30F \u25A6 \U0001F3C1 \U0001F332 \U0001F333 \U0001F334 Test" 

UIAlertView

enter image description here

按钮标题:

@"\u26A0 Yes" 

UITableViewCell与复选框和其他图标

UITableViewCell

+1

令人惊叹,但正如你所说的**你只限于预定义的符号* *但仍然是个好主意,感谢分享这个。 – 2012-03-20 07:42:36

+0

当我们点击这些图片时,我们可以处理事件吗? – Bharathi 2012-03-20 10:36:05

+1

@Jasmine:点击图片实际上是点击按钮本身,因为图片是它的标签。 – 2012-03-20 11:05:57

2

我刚刚创建的类支持使用图像和文本的每一行的表格单元模仿一个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]; 

如果您遇到任何问题,请让我知道。我希望这可以帮助很多有像我一样的人的问题...

1

我知道这是非常晚的答案,但我发现另一种方式来显示实际张图像:

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]; 
+4

不要使用这个,它会在iOS 8中崩溃。在iOS 8上崩溃! – capikaw 2014-10-02 19:40:19

1

我发现这类别扩展在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的帧宽仍然是原始的全尺寸,所以如果你的图像很大(或者更精确的话),那么你会在中心(缩小的)图像和按钮文本之间产生一个烦人的差距。我找不到解决办法。甚至以编程方式为imageview添加显式宽度=高度约束似乎被忽略! [有任何想法吗?]。净结果,确保你的图像是从正确的高度开始的(例如iPhone 4S上的大约45像素),否则你将在按钮图像和文本之间出现越来越大的差距。

  • 更严重的是,只要将图像添加到按钮,UIActionSheet似乎会自动导致按钮的文本为加粗(!)。我不知道为什么,也不知道如何防止这种[任何想法?]

  • 最后,此解决方案依赖于UIActionSheet的子视图与按钮索引的顺序相同。这对于少数按钮来说是正确的,但是当你在UIActionSheet中有很多项目时(比如说),苹果会对索引进行分析[但是在actionSheet中你会遇到这个问题:clickedButtonAtIndex:当你试图找出哪个按钮被窃听...]

呵呵,imageEdgeInsets:是可选的 - 我的插图每个图像的像素夫妇按钮内部,使得图像不垂直相互接触。

[意见:考虑到上述古怪,我感觉苹果真的不想让人们用动作表。在某些时候,你可能不得不咬牙切齿,只实现你自己的模式弹出窗口;只有这么多manultaling这些UIActionSheets将容纳...]

3

您可以通过键“_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]; 
+2

! – 2014-10-04 14:02:49

+2

是的。它不支持在iOS 8 – 2014-10-07 06:54:41

1
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]; 
2

对于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]; 
} 
+0

我试过这段代码,但图像tintcolor更改..... 我怎样才能将原始图像添加到它? – 2015-10-08 09:15:30

3
- (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]; 

}

0

从iOS的8.0你可以使用UIAlertController。在UIAlertController中,每个按钮项都被称为UIAlertAction,它相应地添加。

You can check my answer