How to use UITextView in UIAlertController

后端 未结 6 1367
悲哀的现实
悲哀的现实 2020-12-16 18:48

I created a popup alert using alert controller and added two alert actions(ok and cancel) as below.

UIAlertController * alert=   [UIAlertController
                  


        
6条回答
  •  鱼传尺愫
    2020-12-16 18:58

    With the above solutions my textview was not positioned correctly. This is how I got it to work:

       // The \n is required so that the alertcontroller keeps space for the message. Add as many \n as you like your textview height to be
       self.alertController = [UIAlertController alertControllerWithTitle:@"Some title"
                                                                  message:@"\n\n\n\n\n\n\n\n"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
       self.alertController.view.autoresizesSubviews = YES;
       UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
       textView.translatesAutoresizingMaskIntoConstraints = NO;
       textView.editable = NO;
       textView.dataDetectorTypes = UIDataDetectorTypeAll;
       textView.text = @"Some really long text here";
       textView.userInteractionEnabled = YES;
       textView.backgroundColor = [UIColor clearColor];
       // This will make the scroll view scrollable if the text is too long
       textView.scrollEnabled = YES;
       NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0];
           NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0];
    
       NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64.0];
       NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:64.0];
       [self.retailerHelpAlertController.view addSubview:textView];
       [NSLayoutConstraint activateConstraints:@[leadConstraint, trailConstraint, topConstraint, bottomConstraint]];
    

提交回复
热议问题