Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline editable text UITextview inside UIAlertController?

How can I make a multiline editable text UITextview inside a UIAlertController? UITextfield supports a single line and we need to make a multiline text edit box in the pop up window.

like image 686
Vishal Avatar asked Dec 22 '15 16:12

Vishal


1 Answers

The following code works for Multiline UITextView in UIAlertController, Written in Swift 4

    let alert = UIAlertController(title: "Enter your summary", message: "\n\n\n\n\n\n\n\n", preferredStyle: .alert)
    alert.view.autoresizesSubviews = true

    let textView = UITextView(frame: CGRect.zero)
    textView.translatesAutoresizingMaskIntoConstraints = false

    let leadConstraint = NSLayoutConstraint(item: alert.view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: -8.0)
    let trailConstraint = NSLayoutConstraint(item: alert.view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 8.0)

    let topConstraint = NSLayoutConstraint(item: alert.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: -64.0)
    let bottomConstraint = NSLayoutConstraint(item: alert.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 64.0)
    alert.view.addSubview(textView)
    NSLayoutConstraint.activate([leadConstraint, trailConstraint, topConstraint, bottomConstraint])
    alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
        print("\(String(describing: textView.text))")
    }))
    present(alert, animated: true)
like image 193
Narendra Reddy Avatar answered Sep 25 '22 02:09

Narendra Reddy