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

发布于 2024-11-09 21:16:48 字数 193 浏览 0 评论 0原文

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

Is it possible to add an image to the buttons of the UIActionSheet as seen in UIDocumentInteractionController? If so, please let me know how it is done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

人海汹涌 2024-11-16 21:16:48

可以将图像(准确地说:图标或符号)添加到 UIActionSheet (或 UIAlertView)的按钮,而无需加载图像文件或摆弄 (子)视图。在这些类中,按钮由其标题(字符串)指定。因此,使用符号是显而易见的,也可以通过字符串来指定。我想到的第一个方法是使用 unicode 符号

然后我发现其中有几个在 iOS 上呈现为漂亮的图标,并且可以在 Character 中看到几个符号Mac OS 上的查看器也是如此。因此,这些符号实际上可以在任何可以指定字符串的地方使用。

这种方法的缺点是:

  • 您只能使用预定义的符号。
  • 并非所有符号都按其应有的方式呈现(例如 \u29C9)。
  • 某些符号在不同 iOS 版本上的外观可能会有所不同(例如 iOS 5 和 6 上的 \U0001F533)。

以下是一些有趣的符号:

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

屏幕截图:

UIActionSheet

UIActionSheet

按钮标题:

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

UIAlertView

在此处输入图像描述

按钮标题:

@"\u26A0 Yes"

UITableViewCell 带有复选框和其他图标

UITableViewCell

There is a possibility to add images (to be exact: icons or symbols) to the buttons of a UIActionSheet (or a UIAlertView) without loading image files or fiddling around with (sub)views. In these classes buttons are specified by their titles, which are strings. So it is obvious to use symbols, which one can specify also by strings. The first I came up with was using unicode symbols.

Then I discovered that several of them are rendered as nice icons on iOS and as one can see for several symbols in the Character Viewer on Mac OS, too. Thus, the symbols can be used actually everywhere a string can be specified.

The drawbacks of this approach are:

  • You are limited to predefined symbols.
  • Not all symbols are rendered as they should (e.g. \u29C9).
  • There can be changes in the appearance of some symbols on different iOS versions (e.g. \U0001F533 on iOS 5 and 6).

Here are some interesting symbols among others:

If you want to quickly check how a symbol looks like (at least on Mac OS), you can use the Calculator. Check definitely in the simulator: For instance \u2B1C is not an icon in Calculator 10.7.1.

Screenshots:

UIActionSheet

UIActionSheet

Button titles:

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

UIAlertView

enter image description here

Button title:

@"\u26A0 Yes"

UITableViewCell with checkbox and other icons

UITableViewCell

赢得她心 2024-11-16 21:16:48

试试这个方法,希望对你有帮助。

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

Try this way, i hope it may be help you.

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];
对你再特殊 2024-11-16 21:16:48

标准 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.

The standard UIActionSheet doesn't support images.

One way to add an image to the UIActionSheet is to add a subview to the UIActionSheet. Just implement the UIActionSheetDelegate method willPresentActionSheet: like this:

- (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
}

I'm not sure if the image responds to touches, but you can build a UIActionSheet like theUIDocumentInteractionController.

乞讨 2024-11-16 21:16:48
- (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];

}

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

}

稀香 2024-11-16 21:16:48

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

You can get action button title from actionSheet object by key "_buttons" and set button image.

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];
陪你到最终 2024-11-16 21:16:48

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

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

I just created a class emulating the look of an UIActionSheet using table cells supporting images and text for every row. It also uses blocks for interaction, supports iPhone and iPad, popup from an UITabBarItem on iPad and queueing of multiple sheets. Still in development, but feel free to clone it from Github:

http://github.com/azplanlos/SIActionSheet

Usage is quite simple, here is an example:

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

If you encounter any problems, please let me know. I hope this helps a lot of people having the same problem like me...

一张白纸 2024-11-16 21:16:48

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

For iOS 8, do refer to this

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];
}
小清晰的声音 2024-11-16 21:16:48

我知道这是很晚的答案,但我找到了另一种在操作表中显示图像的方法:

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

I know it's very late answer, but I found another way to show image in action sheet:

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];
我三岁 2024-11-16 21:16:48

我发现这个类别扩展在 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 能够正确地将图像自动调整到按钮的正确高度,但它似乎并没有相应地更改图像视图宽度;因此需要 UIViewContentModeScaleAspectFit 来防止图像被压扁。但是,图像视图框架宽度仍然是原始的全尺寸,因此如果您的图像很大(或更准确地说是宽),那么您会在居中(缩小)的图像和按钮文本之间出现恼人的间隙。我没有找到解决这个问题的方法;即使以编程方式向图像视图添加显式宽度=高度约束似乎也会被忽略!? [有什么想法吗?]。最终结果是,请确保您的图像一开始就具有正确的高度(例如,iPhone 4S 上约为 45 像素),否则按钮图像和文本之间的间隙会越来越大。

  • 更严重的是,只要您向按钮添加图像,UIActionSheet 似乎就会自动使按钮的文本加粗(!)。我不知道为什么,也不知道如何防止这种情况[有什么想法吗?]

  • 最后,此解决方案依赖于 UIActionSheet 的子视图与按钮索引的顺序相同。对于少数按钮来说这是正确的,但是(显然)当你的 UIActionSheet 中有很多项目时,Apple 会搞乱索引 [但是你无论如何都会在 actionSheet:clickedButtonAtIndex: 中遇到问题:当你试图弄清楚时点击了哪个按钮...]

哦,imageEdgeInsets:是可选的 - 我将每个图像插入按钮内几个像素,以便图像不会垂直相互接触。

[观点:鉴于上述奇怪之处,我感觉苹果真的不希望人们乱搞他们的操作表。在某些时候,您可能不得不硬着头皮实现自己的模式弹出窗口;这些 UIActionSheets 只能容纳这么多的人工处理......]

I found this category extension works in ios7.1 to add an image/icon to the buttons in a UIActionSheet, with some caveats...

@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;
            }
        }
    }
}

And to use it:

[self.sharePopup buttonAtIndex:2 setImage:[UIImage imageNamed:@"twitter.png"] forState:UIControlStateNormal];

The caveats:

  • Although the UIActionSheet does correctly autosize your image to the right height for the button, it does not appear to correspondingly change the imageview width; hence the need for the UIViewContentModeScaleAspectFit to prevent the image from getting squished. However, the imageview frame width is still the original full-size, so if your image was big (or more precisely wide) then you'll get an annoying gap between the centered (shrunk) image and the button text. I've found no way around this; even programmatically adding an explicit width=height constraint to the imageview seems to be ignored!? [any ideas?]. Net outcome, make sure your image is about the right height to begin with (eg about 45 pixels on a iPhone 4S) or you'll get an increasingly large gap between the button image and text.

  • More serious, as soon as you add an image to the button, the UIActionSheet seems to automatically cause the button's text to be bolded (!). I dont know why and dont know how to prevent this [any ideas?]

  • Lastly, this solution relies on the UIActionSheet's subviews to be in the same order as the button are indexed. This is true for a handful of buttons, but (apparantly) when you have a lot of items in your UIActionSheet Apple mucks about with the indexing [but you'll have problems with this anyway in actionSheet:clickedButtonAtIndex: when you try to figure out which button was tapped...]

Oh, the imageEdgeInsets: is optional - I inset each image a couple pixels inside the button so that the images dont touch each other vertically.

[Opinion: given the above oddities, I get the feeling Apple really doesn't want people mucking about with their action sheets. At some point you'll probably have to bite-the-bullet and just implement your own modal popup; there's only so much manhandling these UIActionSheets will accommodate...]

§普罗旺斯的薰衣草 2024-11-16 21:16:48
    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];
    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];
格子衫的從容 2024-11-16 21:16:48

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

您可以查看我的答案

From iOS 8.0 you can use UIAlertController. In UIAlertController, each button item is know as UIAlertAction which add accordingly.

You can check my answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文