UIAlertController:添加子视图
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/DYAlertController
我想向我的 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/DYAlertController