如何自定义UIActionSheet?iOS

19

是否可以创建一个类似于此图像的在两个按钮之间带有标签的 ActionSheet

输入图片描述


我认为你必须学会理解代码并按照自己的意愿进行更改。在学习过程中有任何问题可以向我们提问。 - dasdom
7个回答

28

编辑2018

我在iOS4时发布了这段代码,当时可能很有用。自那以后,iOS开发已经变得非常庞大,请不要使用此代码,除非您因某种原因正在编写适用于iOS4的应用程序!请参考精彩的第三方库XLR Action Controller,以满足您对现代操作表的需求!

这肯定是可能的,我经常这样做!

首先,将@property设置为UIActionSheet(在本例中,我的名称为aac——如果您想通过UITextFields调用它,则尤其有用。

接下来,在您带出操作表的方法中,例如-(IBAction)userPressedButton:(id)sender,创建一个局部实例的操作表。

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

然后将其添加到您的属性中:self.aac = actionsheet

现在您基本上可以在此ActionSheet中做任何事情,我将为您提供最近项目中所做内容的缩略版:

- (IBAction)sendDataButtonPressed:(id)sender {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    self.aac = actionSheet;

    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"actionsheet_bg.png"]];
    [background setFrame:CGRectMake(0, 0, 320, 320)];
    background.contentMode = UIViewContentModeScaleToFill;
    [self.aac addSubview:background];

    UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(0, 260, 320, 50);
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"actionsheet_button.png"] forState: UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked:)    forControlEvents:UIControlEventTouchUpInside];
    cancelButton.adjustsImageWhenHighlighted = YES;
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateNormal];
    cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    cancelButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 25];

    [self.aac addSubview: cancelButton];

    UIButton *emailResultsButton = [UIButton buttonWithType: UIButtonTypeCustom];
    emailResultsButton.frame = CGRectMake(25, 12, 232, 25);
    [emailResultsButton addTarget:self action:@selector(emailResultsTapped:) forControlEvents:UIControlEventTouchUpInside];
    emailResultsButton.adjustsImageWhenHighlighted = YES;
    [emailResultsButton setTitle:@"Email Results" forState:UIControlStateNormal];
    [emailResultsButton setTitleColor:[UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [emailResultsButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateHighlighted];
    emailResultsButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    emailResultsButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 20];
    [self.aac addSubview: emailResultsButton];

// lots of other buttons...

// then right at the end you call the showInView method of your actionsheet, and set its counds based at how tall you need the actionsheet to be, as follows:

[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 600)];

以下是发生的情况的图片(请记住,我省略了代码示例中的所有其他按钮,但它们在这里显示):enter image description here

现在,如果你想关闭操作表-根据你如何设置按钮结构,或者可能使用一个UIToolBar(非常常见)- 你可以在按钮的选择器操作中执行以下代码:

-(void)cancelButtonClicked:(id)sender { [self.aac dismissWithClickedButtonIndex:0 animated:YES]; }

还有一点需要注意,调整好布局的所有尺寸会花费一些时间,因为你不是在主UIView的视图上工作,而是在操作表的视图上工作,所以具有不同的尺寸。

我使用的一个技巧是,在Storyboard中使用UIView来布置操作表,然后把你想要放到“操作表”中的所有对象都放在那个UIView中,这样你就可以获得所有图像的准确尺寸。

如果需要澄清,请告诉我,祝好运!


2
这段代码在IOS 7中产生以下错误:CGContextSetFillColorWithColor: invalid context 0x0。这是一个严重的错误。该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的总体降级。此通知是一种礼貌:请解决此问题。它将在即将到来的更新中成为致命错误。 - 1110
我得到了和1110一样的错误,总是在自定义操作表时发生。 - ggfela
2
要消除该错误,请将标题字符串设置为@""。 - Plague
@ggfela 很好的修复!但是,只是为了让大家知道,当我设置标题时,在操作表中布置背景时,我注意到了一些图形/绘图问题。仅供参考。 - Maximilian
4
使用操作表的意义是什么?为什么不只是创建一个UIView并对其进行动画处理? - James Parker
显示剩余3条评论

8

XLActionController允许我们创建任何自定义操作表,比上面提供的解决方案更具灵活性。

以下是github存储库中包含的一些示例。


@WebberLai 我几乎可以确定它不会工作,因为它使用了 Swift 特定的功能。 - mtnBarreto

3

我本来只想添加一条小评论,但我还不能发表评论,所以我会发表完整的答案。如果你想避免IOS7错误CGContextSetFillColorWithColor: invalid context 0x0.这是一个严重的错误等等。你可以使用以下方法。

self.actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                               delegate:nil
                                      cancelButtonTitle:nil
                                 destructiveButtonTitle:nil
                                      otherButtonTitles:nil];

然而,正如@Max所说,这样做会破坏您操作表顶部的图形/绘图,因此如果您还没有背景,则只需添加此代码即可获得默认操作表外观。

UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
[self.actionSheet addSubview:background];

然后添加您想要的任何其他内容到您的自定义操作表中。


2

1
UIButton* button = (UIButton*)sender;
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share the PNR Status" delegate:self
                                     cancelButtonTitle:@"Cancel"
                                destructiveButtonTitle:nil
                                     otherButtonTitles:@"Send Message",@"Send Email",@"Facebook",nil];

    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"chat.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"facebook_black.png"] forState:UIControlStateNormal];
 //*********** Cancel
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
    [actionSheet showFromRect:button.frame inView:self.view animated:YES];

这是使用私有API,将会被苹果拒绝。 - Pion
2
我的应用程序运行良好,已经在应用商店上架并且已经被苹果批准。 - mahendra
是的,但它不被支持,并且如果他们更新实现,可能会导致代码出错。 - Miguel Carvajal

0

0
如果您仍然在苦苦挣扎,我有一个可能会对您有所帮助。它允许您创建自定义操作表。它具有大量内置类型,也可以进行扩展和重新样式化。

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