我想向我的警报控制器添加一个子视图。但为什么按钮要放在顶部呢?我该如何解决这个问题?
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 https://github.com/DominikButz/DYAlertController
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)