UIAlertController:addSubview

我想在我的alertcontroller中添加一个子视图。 但为什么按钮会出现在顶部? 我该如何解决这个问题?

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:{}) 

在此处输入图像描述

UIAlertController是一个非常封闭的系统。 它旨在成为系统标准警报。 您不应该向其添加子视图。

我会创建一个可以充当警报的自定义UIViewController。 您可以使用自定义UIViewController转换使其显示方式与UIAlertController相同。

还有许多GitHub项目提供您可能喜欢的自定义警报样式。 比如这个: https : //github.com/DominikButz/D​​YAlertController