添加文本字段 MDCAlertController IOS

Add Textfield MDCAlertController IOS

Uialertcontroller中没有类似addtextfield方法的方法。我还没有找到任何有关如何自定义 MDCAlertControllers 的示例。有人有想法吗?

我认为这是不可能的。 docs 说:

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.

现在可以使用 accessoryView
只需将其设置为您的自定义视图并将警报消息设置为空白。

alertController.accessoryView = myCustomView


代码示例(使用 SnapKit 进行布局)

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)

上面代码的结果