Ios 在Objective-C中的UIAlertController下添加Textview

Ios 在Objective-C中的UIAlertController下添加Textview,ios,objective-c,uitextview,uialertcontroller,Ios,Objective C,Uitextview,Uialertcontroller,如何在UIAlertController下添加文本视图?我尝试了下面的方法,但是文本视图没有正确显示 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Comments" message:@"Enter your submit comments" preferredStyle:UIAlertControllerStyleAlert]; alertController.view.au

如何在
UIAlertController
下添加文本视图?我尝试了下面的方法,但是文本视图没有正确显示

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Comments" message:@"Enter your submit comments" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.autoresizesSubviews = YES;

UITextView * alertTextView = [[UITextView alloc] initWithFrame:CGRectZero];
alertTextView.translatesAutoresizingMaskIntoConstraints = NO;
alertTextView.backgroundColor = [UIColor greenColor];

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

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

UIAlertAction *okButtonAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
    [alertTextView resignFirstResponder];

}];
UIAlertAction *cancelButtonAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

}];
[alertController addAction:okButtonAction];
[alertController addAction:cancelButtonAction];

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

用这个代码检查。这对我来说很好

self.alertController = [UIAlertController alertControllerWithTitle:@"Some title"
                                                           message:@"Last year, in addition to the major redesign, we also got Maps apps, so everything from ride-sharing to reservation making could live in and enhance maps. We also got steady progress for Apple Maps on the web.expect we'll see some of the next-generation of Maps updates at WWDC 2017 but here's hoping Apple can shed its dependence on TomTom data, accelerate the updates, and start closing that gap."
                                                    preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
                     actionWithTitle:@"OK"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                         [self.alertController dismissViewControllerAnimated:YES completion:nil];

                     }];
self.alertController.view.autoresizesSubviews = YES;
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.translatesAutoresizingMaskIntoConstraints = NO;
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.userInteractionEnabled = YES;
textView.backgroundColor = [UIColor clearColor];
textView.scrollEnabled = YES;
[self.alertController addAction:ok];
[self presentViewController:self.alertController animated:YES completion:nil];

请查看此代码。测试

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Some title"
                                                                         message:@"\n\n\n\n\n\n\n\n"
                                                                  preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okay = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                           handler:^(UIAlertAction * action) {
                                               //Do Some action here
                                           }];
UIAlertAction* cancel1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                               }];
[alertController addAction:okay];
[alertController addAction:cancel1];

alertController.view.autoresizesSubviews = YES;
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.translatesAutoresizingMaskIntoConstraints = NO;
textView.editable = YES;
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.text = @"Some really long text here";
textView.userInteractionEnabled = YES;
textView.backgroundColor = [UIColor whiteColor];
textView.scrollEnabled = YES;
NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0];
NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0];

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

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

}];

自定义
UIAlertController
可能存在重复。你不应该这样玩
UIAlertController
。没有任何东西可以保证它在下一个版本中能正常工作,不要在其中添加子视图。你检查过这个答案吗?@Ben10