2017-05-09 53 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我刪除了我的舊回答 –