如何自定义UIActionSheet?的iOS [英] How to customize UIActionSheet? iOS

查看:74
本文介绍了如何自定义UIActionSheet?的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建在两个按钮(如该图像)之间具有标签的ActionSheet?

Is it possible to create an ActionSheet that have a label between two buttons like this image?

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

Really need this right now. Can anyone help me? Thanks.

推荐答案

EDIT 2018

当此代码可能有用时,我一直将此代码一直发布到iOS4中.自那时以来,iOS开发已取得惊人的增长,除非出于任何原因为iOS4编写应用程序,否则请不要使用此代码!请参阅精彩的第三方库 XLR操作控制器,以了解您的标准操作表!

I posted this code all the way back in iOS4 when it was potentially useful. iOS Development has grown staggeringly since then, please do not use this code unless you are writing an app for iOS4 for whatever reason! Please refer to the wonderful 3rd party library XLR Action Controller for your mondern actionsheet needs!

这肯定是有可能的,我一直很喜欢这样做!

It is most certainly possible, and I like to do this all the time!

首先,将UIActionSheet制成@property(在此示例中,我的名字叫aac-派上用场,特别是如果您想用UITextFields调用它.

First, make a @property of an UIActionSheet (in this example, mine is called aac-- this comes in handy especially if you want to call it by UITextFields.

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

Next, in the method you bring up the actionsheet, say a -(IBAction)userPressedButton:(id)sender, create a local instance of the actionsheet.

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

然后将其添加到您的媒体资源:self.aac = actionsheet

then add it to your property: self.aac = actionsheet

现在,您基本上可以在此ActionSheet中做任何事情,我将为您提供我最近在一个项目中所做的工作的缩写形式:

Now you basically have full reign to do anything in this ActionSheet, I will give you an abbreviated form of what I did for a recent project:

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

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

Here is a picture of what happens (remember I omitted all the other buttons in the code example, but they are pictured here):

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

Now, if you want to dismiss the actionSheet -- depending on how you set up your button structure, or perhaps use a UIToolBar (very common) -- you can then in the button's selector action, do this:

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

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

此外,仅供参考,要使所有尺寸恰好适合您的布局将需要一些时间,因为您没有使用UIViewController主UIView的视图,而是actionSheet的视图,因此它具有不同的尺寸.

Also, just FYI, getting all the dimensions just right for your layout will take a little time, as you aren't working with the view of your main UIView of your UIViewController, rather the view of the actionSheet, so it has different dimensions.

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

One trick I do is layout the actionsheet in Storyboard with a UIView, then place all the objects you want in your "actionsheet" in that UIView, and you should get accurate dimensions for all the images.

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

Let me know if you need clarification, good luck!

这篇关于如何自定义UIActionSheet?的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆