0

I have a UITextView inside UIAlertViewController. I need to set leading and trailing constraints to UITextView programatically.

Screenshot

enter image description here

Here is my code

func popUpController()
{
    let alert = UIAlertController(title: "Additional Notes", message: "Write your additional notes here.", preferredStyle: .alert)

    let textView = UITextView()
    textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    let color = UIColor(red: 224/255, green: 224/255, blue: 224/255, alpha: 1.0).cgColor

    textView.layer.borderColor = color
    textView.layer.borderWidth = 1.0
    textView.layer.cornerRadius = 5.0

    let controller = MyActivitiesViewController()

    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: 180)
    alert.view.addConstraint(height)

    let saveAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alert.addAction(saveAction)
    alert.addAction(cancelAction)


    self.present(alert, animated: true, completion: {

        textView.becomeFirstResponder()
    })

}
MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
  • According to Apple docs: `**Important** The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.` You are much better off presenting your own controller instead of trying to do something you're explicitly *not* supposed to do. – DonMag Jul 24 '18 at 13:37

1 Answers1

1

Please note that this only a work around.

If you want to give more height to the alertcontroller add more new line to title.

Adjust the constraints according to your requirement if you wish to modify this

 func showAlertController()
    {

        let alertController = UIAlertController(title: "Your title \n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        let customView = UITextView()
        customView.translatesAutoresizingMaskIntoConstraints = false
        alertController.view.autoresizesSubviews = true
        let somethingAction = UIAlertAction(title: "Some Action", style: UIAlertActionStyle.default, handler: {(alert: UIAlertAction!) in

            print(customView.text)

        })

        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

        alertController.addAction(somethingAction)
        alertController.addAction(cancelAction)


        alertController.view.addSubview(customView)

        customView.topAnchor.constraint(equalTo: alertController.view.topAnchor, constant: 50).isActive = true

        customView.rightAnchor.constraint(equalTo: alertController.view.rightAnchor, constant: -10).isActive = true
        customView.leftAnchor.constraint(equalTo: alertController.view.leftAnchor, constant: 10).isActive = true
        customView.bottomAnchor.constraint(equalTo: alertController.view.bottomAnchor, constant: -60).isActive = true
        customView.backgroundColor = UIColor.red
        self.present(alertController, animated: true) {

        }

    }

enter image description here

Anuraj
  • 1,242
  • 10
  • 25