2017-05-09 71 views
0

Apple审查允许下面的代码,即添加一个textView或标签作为“accessoryView”..我读了很多地方,它不是一个公众暴露的API,我们不应该这样做..但很少说我们可以..当我们发送评论时它会被拒绝吗?请指导我...将UITextView或UILabel添加为UIAlertView作为“accessoryView”?

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"]; 
+0

你在最新的iOS版本的工作?如果是,则请使用AlertController显示警报。这里有几个可以帮助的链接http://stackoverflow.com/questions/31922349/how-to-add-textfield-to-uialertcontroller-in-swift http://stackoverflow.com/questions/37696485/show-the -textfield-in-the-alertcontroller-in-swift –

+0

感谢您的帮助。是的,我正在使用最新版本的iOS。但我想添加一个textview而不是一个文本框。 – DivGoogly

+0

好的..我建议去多线textfield作为在这个链接http://stackoverflow.com/questions/34420080/multiline-editable-text-uitextview-inside-uialertcontroller也看看这个链接http://stackoverflow.com/问题/ 28603060 /如何使用uitextview-in-uialertcontroller –

回答

0

有关警报添加文本视图中使用此代码

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

}]; 
+1

不要写在不同的答案,作出一个问题的单一答案。 –

+0

@ dahiya_boy我删除了我的旧回答 –