6

There is no method similar to the addtextfield method in Uialertcontroller. I haven't found any examples of how I can customize MDCAlertControllers. Anybody have an idea?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ulaserdegor
  • 371
  • 5
  • 10

2 Answers2

3

I think this is not possible. The docs say:

MDCAlertController 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.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
1

This is now possible by using accessoryView.
Simply set it to be your custom view and set the alert message to be blank.

alertController.accessoryView = myCustomView


Code example (using SnapKit for layout)

let title = "My title"        
let alertController = MDCAlertController(title: title, message: "")
let confirmAction = MDCAlertAction(title:"Confirm") { [weak self] _ in
    //your action here
}
let cancelAction = MDCAlertAction(title:"Cancel")

let width = UIScreen.main.bounds.width * 0.91
let testView = UIView()
let button = UIButton()
button.setTitle("Test", for: .normal)
testView.addSubview(button)
button.snp.makeConstraints { (make) in
    make.center.equalToSuperview()
}
testView.backgroundColor = .blue //just to show where the view is
testView.snp.makeConstraints { (make) in
    make.width.equalTo(width)
    make.height.equalTo(100)
}

alertController.accessoryView = testView
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
alertController.defaultTheming()

present(alertController, animated:true)

Result from above code

enter image description here

Markymark
  • 2,804
  • 1
  • 32
  • 37