Reputation: 416
I want to add a subview to my alertcontroller. But why do the buttons go on the top ? How do I fix the problem?
let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.alert)
let somethingAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("something")})
let cancelAction = UIAlertAction(title: "Annuler", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})
alert.addAction(somethingAction)
alert.addAction(cancelAction)
let customView = UIView()
customView.backgroundColor = .green
customView.translatesAutoresizingMaskIntoConstraints = false
customView.widthAnchor.constraint(equalToConstant: 128).isActive = true
customView.heightAnchor.constraint(equalToConstant: 128).isActive = true
alert.view.addSubview(customView)
customView.centerXAnchor.constraint(equalTo: alert.view.centerXAnchor).isActive = true
customView.topAnchor.constraint(equalTo: alert.view.topAnchor).isActive = true
customView.bottomAnchor.constraint(equalTo: alert.view.bottomAnchor, constant: -32).isActive = true
self.present(alert, animated: true, completion:{})
Upvotes: 2
Views: 7202
Reputation: 18551
UIAlertController is a pretty closed system. Its designed to be the system standard alert. You should not be adding subviews to it.
I would create an custom UIViewController that can act as an alert. You can use the custom UIViewController transition to make appear the same way UIAlertController does.
There are also many GitHub projects that provide custom alert styles that you might like. Such as this one: https://github.com/DominikButz/DYAlertController
Upvotes: 1