带有多行UITextField的iOS AlertController

时间:2017-04-24 14:41:39

标签: ios objective-c uialertcontroller

我正在开发一个应用程序,但这是一个相当古老的应用程序。客户不想花太多时间,所以我正在寻找一个简单快速的解决方案来解决这个问题。

用户必须为他们的个人资料编写摘要,因此他们需要一个很大的空间来写入。整个应用程序是使用AlertController弹出窗口构建的,所以我想用更大的多行UITextField替换单行UITextField

我能做些什么来实现这一目标? 我目前添加了以下代码:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"Enter your summary" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Summary";
        textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
        textField.text = _summaryLabel.text;

        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant: 110.0];
        [textField addConstraint:constraint];
        [textField layoutIfNeeded];
    }];

这意味着UITextField实际上更大,但它不是多行,它也是垂直居中的文本。我该怎么办?

3 个答案:

答案 0 :(得分:6)

正如@rmaddy所说,UITextField仅适用于单行,您可以使用UITextView

你可以通过这个来实现这个目标:

How to use UITextView in UIAlertController

UIAlertController *alert = [UIAlertController
                            alertControllerWithTitle:@"Enter your summary"
                            message:@"\n\n\n\n\n\n\n\n"
                            preferredStyle:UIAlertControllerStyleAlert];
alert.view.autoresizesSubviews = YES;

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0];
NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0];

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64.0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:64.0];
[alert.view addSubview:textView];
[NSLayoutConstraint activateConstraints:@[leadConstraint, trailConstraint, topConstraint, bottomConstraint]];

[alert addAction: [UIAlertAction actionWithTitle:@"Done"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
    NSLog(@"%@", textView.text);
}]];

[self presentViewController:alert animated:YES completion:nil];

UIAlertController

或使用UIAlertView

How to Insert the UITextView into UIAlertview in iOS

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"Enter your summary"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Done", nil];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
[alert setValue:textView forKey:@"accessoryView"];
[alert show];

UIAlertView

或者自定义您自己的提醒,例如ios-custom-alertview

答案 1 :(得分:1)

您可以在AlertController中嵌入ViewController。

-(void)showAlertWithTitle:(NSString *)title andBody:(NSString *)body andTextToEdit:(NSString*)text{

    UIViewController *controller = [[UIViewController alloc]init];

    // Some size
    CGRect rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];

    // The text view to be used
    UITextView *textView = [[UITextView alloc]initWithFrame:rect];

    [textView setText:text]; // Original text is there to edit
    [controller.view addSubview:textView];  // Add the textField to the controller

    // I needed these when adding a UITableView, not sure if needed for UITextView
    [controller.view bringSubviewToFront:textView];
    [controller.view setUserInteractionEnabled:YES];

    alertWithText = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert];
    [alertWithText setValue:controller forKey:@"contentViewController"];

    // create the actions handled by each button
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"The Edited Text : %@", textView.text);
    }];

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

    }];


        // add the actions to our alert
    [alertWithText addAction:action1];
    [alertWithText addAction:action2];

    [self presentViewController:alertWithText animated:YES completion:^{

    }];
}

答案 2 :(得分:0)

万一有人需要上面的@tomfriwel答案,但是在Swift中:

    let alert = UIAlertController (title: "Enter your summary", message: "\n\n\n\n\n\n\n\n", preferredStyle: .alert)
    alert.view.autoresizesSubviews = true

    let textView = UITextView (frame: .zero)
    
    textView.translatesAutoresizingMaskIntoConstraints = false

    let leadConstraint = NSLayoutConstraint (item: alert.view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: -8.0)

    let trailConstraint = NSLayoutConstraint (item: alert.view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 8.0)

    let topConstraint = NSLayoutConstraint (item: alert.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: -64.0)

    let bottomConstraint = NSLayoutConstraint (item: alert.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 64.0)

    alert.view.addSubview(textView)
    
    NSLayoutConstraint.activate([leadConstraint, trailConstraint, topConstraint, bottomConstraint])

    let Done = UIAlertAction(title: "Done", style: .default) { (action:UIAlertAction) in
        NSLog("%@", textView.text)
    }
    alert.addAction(Done)


    self.present(alert, animated: true, completion: nil)