Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UITextView in UIAlertController

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.

like image 214
vijay Avatar asked Feb 19 '15 09:02

vijay


2 Answers

Johno2110's solution worked best for me with a little cleanup and tweaking. Posting the code and a screenshot of the result here in case it helps anybody else out.

Confirmed working with Swift 5.

let textView = UITextView(frame: CGRect.zero)

@IBAction func sendFeedback(_ sender: Any) {
    let alertController = UIAlertController(title: "Feedback \n\n\n\n\n", message: nil, preferredStyle: .alert)

    let cancelAction = UIAlertAction.init(title: "Cancel", style: .default) { (action) in
        alertController.view.removeObserver(self, forKeyPath: "bounds")
    }
    alertController.addAction(cancelAction)

    let saveAction = UIAlertAction(title: "Submit", style: .default) { (action) in
        let enteredText = self.textView.text
        alertController.view.removeObserver(self, forKeyPath: "bounds")
    }
    alertController.addAction(saveAction)

    alertController.view.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil)
    textView.backgroundColor = UIColor.white
    textView.textContainerInset = UIEdgeInsets.init(top: 8, left: 5, bottom: 8, right: 5)
    alertController.view.addSubview(self.textView)

    self.present(alertController, animated: true, completion: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "bounds"{
        if let rect = (change?[NSKeyValueChangeKey.newKey] as? NSValue)?.cgRectValue {
            let margin: CGFloat = 8
            let xPos = rect.origin.x + margin
            let yPos = rect.origin.y + 54
            let width = rect.width - 2 * margin
            let height: CGFloat = 90

            textView.frame = CGRect.init(x: xPos, y: yPos, width: width, height: height)
        }
    }
}

alert

like image 71
Adam Kaump Avatar answered Sep 20 '22 16:09

Adam Kaump


Swift 3

 let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
 let textView = UITextView()
 textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

 let controller = UIViewController()

 textView.frame = controller.view.frame
 controller.view.addSubview(textView)

 alert.setValue(controller, forKey: "contentViewController")

 let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: view.frame.height * 0.8)
 alert.view.addConstraint(height)

 present(alert, animated: true, completion: nil)
like image 27
Woody Jean-louis Avatar answered Sep 17 '22 16:09

Woody Jean-louis