ios - 在 Objective-C 的 UIAlertController 下添加 Textview

标签 ios objective-c uitextview uialertcontroller

如何在 UIAlertController 下添加 TextView ?我尝试了下面的方法,但 TextView 显示不正确。

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];

最佳答案

查看此代码。已测试

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:^{

}];

关于ios - 在 Objective-C 的 UIAlertController 下添加 Textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789708/

相关文章:

iphone - ios TreeView 组件的数据模型

iOS - 如何预定义 TextView 行高

iphone - 向单元格添加阴影会导致滚动延迟

iphone - 安装时将数据传递到我的应用程序

ios - 在 UITableView - Realm 中将结果转换为数据源列表时出错

ios - Swift 替换子字符串正则表达式

ios - 向 UIView 添加多个 subview

objective-c - 如何通过 UIImagePNGRepresentation() 加速将 UIImagePickerController 图像从相机保存到文件系统?

ios - 如何在 UITextView 中显示可点击的链接

ios - 如何检测UITextView的自动更正状态?