【问题标题】:Adding a UITextView or UILabel to UIAlertView as "accessoryView"?将 UITextView 或 UILabel 添加到 UIAlertView 作为“accessoryView”?
【发布时间】:2017-05-09 04:47:16
【问题描述】:

Apple 审查是否允许以下​​编码,即添加 textView 或标签作为“accessoryView”.. 我在很多地方读到它不是公开的 api,我们不应该这样做.. 但很少有人说我们可以。 .. 在我们发送审核时它会被拒绝吗?请指导我...

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
textView.selectable = YES;
textView.editable = NO;
textView.attributedText = attributedStr;
UIAlertView *customAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[customAlert setValue:textView forKey:@"accessoryView"];

【问题讨论】:

  • 您正在使用最新的 iOS 版本吗?如果是,那么请使用 AlertController 来显示警报。这里有几个链接可能会有所帮助stackoverflow.com/questions/31922349/…stackoverflow.com/questions/37696485/…
  • 感谢您的帮助。是的,我使用的是最新版本的 iOS。但我想添加一个文本视图而不是文本字段。
  • 好的..我建议在这个链接stackoverflow.com/questions/34420080/…中使用多行文本字段,也看看这个链接stackoverflow.com/questions/28603060/…
  • 我可以看到他们已经使用“addSubView”到 UIAlertController 来添加文本视图。这是正确的方法吗?
  • 你得到想要的结果了吗?如果是,那么在任何控制器的视图上添加视图都没有害处。 UIAlertcontroller 也是控制器,不像 AlertView。

标签: ios objective-c


【解决方案1】:

要在警报上添加文本视图,请使用此代码

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Some title"
                                                                         message:@"\n\n\n\n\n\n\n\n"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
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:^{

}];

【讨论】:

  • 不要写不同的答案,对问题做一个单一的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多