gpt4 book ai didi

带有多行 UITextField 的 iOS AlertController

转载 作者:可可西里 更新时间:2023-11-01 05:17:54 25 4
gpt4 key购买 nike

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

用户必须为他们的个人资料写一个摘要,所以他们需要很大的空间来写。整个应用程序是用 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 实际上更大,但它不是多行的,而且它还在垂直居中文本。我该怎么办?

最佳答案

正如@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

关于带有多行 UITextField 的 iOS AlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43591106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com