首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用UITextFields和UITextViews定制UIAlertView

使用UITextFields和UITextViews定制UIAlertView
EN

Stack Overflow用户
提问于 2012-12-02 21:47:29
回答 4查看 2.5K关注 0票数 0

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

EN

回答 4

Stack Overflow用户

发布于 2012-12-02 23:00:43

你真的不能制作一个自定义的警告视图,因为苹果已经决定这是他们不想让我们搞乱的东西。

代码语言:javascript
运行
复制
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[myAlertView show];

但是,如果您确实希望进行这些更改,则必须在UIView的基础上进行自己的更改,并将其装扮成类似于警报视图的样子。下面是一个粗略的例子:

代码语言:javascript
运行
复制
- (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];
    }];
}
票数 4
EN

Stack Overflow用户

发布于 2012-12-02 22:06:38

执行相同的操作,并使用多个'\n‘字符作为消息文本,以便向下移动按钮。

例如:

代码语言:javascript
运行
复制
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];
票数 0
EN

Stack Overflow用户

发布于 2012-12-02 22:07:10

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

代码语言:javascript
运行
复制
@"The alert message.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13669871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档