0

我想制作一个UIAlertView其中有一个UITextFieldUITextView显示大约 5 - 6 行的内容。我尝试创建视图并将其添加为子视图,但它与警报视图的按钮重叠。在调整警报视图的大小时,按钮不会向下移动。我还需要为它设置不同的背景和东西。那就是我需要制作一个自定义警报视图。我是 iPhone 编程的新手。请提供一种方法来做到这一点。

4

4 回答 4

4

你真的不能制作自定义警报视图,因为 Apple 已经决定这是他们不希望我们搞砸的事情。如果您只能在警报中使用一个文本字段,并且可以使用股票背景颜色setAlertViewStyle:UIAlertViewStylePlainTextInput将文本字段放入警报中。

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[myAlertView show];

但是,如果您确实想要进行这些更改,则必须自己制作UIView并修饰它以使其看起来像警报视图。这是一个粗略的例子:

- (IBAction)customAlert:(UIButton *)sender
{
    UIView *myCustomView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 300)];
    [myCustomView setBackgroundColor:[UIColor colorWithRed:0.9f green:0.0f blue:0.0f alpha:0.8f]];
    [myCustomView setAlpha:0.0f];

    UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [dismissButton addTarget:self action:@selector(dismissCustomView:) forControlEvents:UIControlEventTouchUpInside];
    [dismissButton setTitle:@"Close" forState:UIControlStateNormal];
    [dismissButton setFrame:CGRectMake(20, 250, 240, 40)];
    [myCustomView addSubview:dismissButton];

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 240, 35)];
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    [myCustomView addSubview:textField];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 75, 240, 150)];
    [myCustomView addSubview:textView];

    [self.view addSubview:myCustomView];

    [UIView animateWithDuration:0.2f animations:^{
        [myCustomView setAlpha:1.0f];
    }];
}

- (void)dismissCustomView:(UIButton *)sender
{
    [UIView animateWithDuration:0.2f animations:^{
        [sender.superview setAlpha:0.0f];
    }completion:^(BOOL done){
        [sender.superview removeFromSuperview];
    }];
}
于 2012-12-02T15:00:42.820 回答
0

做同样的事情,为了向下移动按钮,使用多个 '\n' 字符作为消息文本。

例如:

UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle: @"Your title"
        message: @"\n\n\n\n\n\n\n\n\n\n"
        delegate: nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
[alert show];
[alert release];
于 2012-12-02T14:06:37.900 回答
0

对于警报消息文本,只需添加一堆换行符,如下所示:

@"The alert message.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
于 2012-12-02T14:07:09.827 回答
0

好吧,最好的方法是创建一个类,它是 的子类UIAlertView,但UIAlertView类引用说UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

我需要创建一个弹出登录已经有一段时间了,我按照本教程进行操作。它非常有用,然后我阅读了一些帖子,人们抱怨他们的应用程序被拒绝,因为使用UIAlertView. 有趣的是,当时 youtube 应用正在使用一个弹出登录控件,看起来他们使用了 的子类UIAlertView,但也许他们从头开始创建了UIView. 我认为值得一试。

于 2012-12-02T15:05:00.127 回答