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.
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)
}
}
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With