How to use UITextView in UIAlertController

vijay  picture vijay · Feb 19, 2015 · Viewed 19.5k times · Source

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

UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:@"Cycling"
                              message:@"Please enter title and description"
                              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
                     actionWithTitle:@"OK"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                         [alert dismissViewControllerAnimated:YES completion:nil];

                     }];
UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];

Now, i want add UITextView. Because I have two text field like title and description. For description i want to use UITextView for adding no.of lines. I tried i am not getting how to add it.

Please advice.

Answer

Ben picture Ben · Feb 17, 2016

Adding UITextView to UIAlertController:

https://gist.github.com/bennagar/c0cd618bcd23c4c2dadf

func showAlert() {

    let saveAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

    saveAction.enabled = false

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

    alertController.view.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.New, context: nil)

    NSNotificationCenter.defaultCenter().addObserverForName(UITextViewTextDidChangeNotification, object: textView, queue: NSOperationQueue.mainQueue()) { (notification) in
        saveAction.enabled = self.textView.text != ""
    }

    textView.backgroundColor = UIColor.greenColor()
    alertController.view.addSubview(self.textView)

    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, completion: nil)

}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "bounds"{
        if let rect = (change?[NSKeyValueChangeNewKey] as? NSValue)?.CGRectValue(){
            let margin:CGFloat = 8.0
            textView.frame = CGRectMake(rect.origin.x + margin, rect.origin.y + margin, CGRectGetWidth(rect) - 2*margin, CGRectGetHeight(rect) / 2)
            textView.bounds = CGRectMake(rect.origin.x + margin, rect.origin.y + margin, CGRectGetWidth(rect) - 2*margin, CGRectGetHeight(rect) / 2)
        }
    }
}

<script src="https://gist.github.com/bennagar/c0cd618bcd23c4c2dadf.js"></script>

Still locking for way to get the height of the marked view, when I have it I can just replace the /2 with the correct height. enter image description here