我想制作一个UIAlertView
,其中有一个UITextField
和UITextView
,显示大约5-6行。我尝试创建视图并将其添加为子视图,但它与警报视图的按钮重叠。调整警报视图的大小时,按钮不会向下移动。我也需要为它设置不同的背景和东西。这就是说,我需要创建一个自定义的警告视图。我是iPhone编程的新手。请提供一种方法来完成此操作。
发布于 2012-12-02 23:00:43
你真的不能制作一个自定义的警告视图,因为苹果已经决定这是他们不想让我们搞乱的东西。
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-02 22:06:38
执行相同的操作,并使用多个'\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-02 22:07:10
对于警告消息文本,只需添加一串换行符,如下所示:
@"The alert message.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
https://stackoverflow.com/questions/13669871
复制相似问题