我想给我的警报控制器添加一个子视图。但是为什么按钮在顶部呢?我该怎么解决这个问题?
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:{})

发布于 2017-03-24 15:04:01
UIAlertController是一个非常封闭的系统。它被设计成系统标准警报。您不应该向它添加子视图。
我将创建一个可以充当警报的自定义UIViewController。您可以使用自定义UIViewController转换使其看起来与UIAlertController一样。
还有许多GitHub项目提供您可能喜欢的自定义警报样式。比如这个:https://github.com/DominikButz/DYAlertController
https://stackoverflow.com/questions/43002236
复制相似问题