Multiline editable text UITextview inside UIAlertController?
This is good appraoch ...
func popUpController()
{
let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
let customView = UITextView(frame: rect)
customView.backgroundColor = UIColor.clear
customView.font = UIFont(name: "Helvetica", size: 15)
// customView.backgroundColor = UIColor.greenColor()
alertController.view.addSubview(customView)
let somethingAction = UIAlertAction(title: "Something", style: UIAlertAction.Style.default, handler: {(alert: UIAlertAction!) in print("something")
print(customView.text)
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: {(alert: UIAlertAction!) in print("cancel")})
alertController.addAction(somethingAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion:{})
}
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)