如何自定义UIActionSheet?iOS版

car*_*ean 19 objective-c uiactionsheet

是否可以创建一个ActionSheet在这个图像之间的两个按钮之间有标签的?

在此输入图像描述

现在真的需要这个.谁能帮我?谢谢.

Max*_*Max 27

编辑2018年

我在iOS4中发布此代码时可能会有用.iOS开发从那时起已经发展壮大,请不要使用此代码,除非您出于任何原因为iOS4编写应用程序!请参考精彩的第三方库XLR Action Controller,以满足您的现代操作表需求!

这当然是可能的,我喜欢一直这样做!

首先,做一个@propertyUIActionSheet(在这个例子中,我的叫aac-这个就派上用场了,特别是如果你想调用它UITextFields.

接下来,在您打开动作表的方法中,比如说 -(IBAction)userPressedButton:(id)sender,创建动作表的本地实例.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
Run Code Online (Sandbox Code Playgroud)

然后将其添加到您的财产: 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)];
Run Code Online (Sandbox Code Playgroud)

这是一张会发生什么的图片(记得我省略了代码示例中的所有其他按钮,但是这里有图片): 在此输入图像描述

现在,如果要关闭actionSheet - 取决于您设置按钮结构的方式,或者使用UIToolBar(非常常见) - 您可以在按钮的选择器操作中执行以下操作:

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

另外,仅仅是因为你的布局所需的所有尺寸都需要一点时间,因为你没有使用你的UIViewController的主UIView的视图,而是使用actionSheet的视图,所以它有不同的尺寸.

我做的一个技巧是使用UIView在Storyboard中布置操作表,然后将所需的所有对象放在UIView中的"actionsheet"中,并且您应该获得所有图像的准确尺寸.

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

  • 此时使用行动表有什么意义?为什么不直接创建一个UIView动画呢? (4认同)
  • 此代码在IOS 7中产生以下错误:CGContextSetFillColorWithColor:无效的上下文0x0.这是一个严重的错误.该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低.此通知是礼貌的:请解决此问题.它将成为即将到来的更新中的致命错误. (2认同)
  • @ggfela删除该错误只是将标题字符串设置为@"". (2认同)

mtn*_*eto 8

https://github.com/xmartlabs/XLActionController允许我们创建任何自定义操作表,提供比上面提出的解决方案更多的灵活性.

这些是github存储库中包含的一些示例.