gpt4 book ai didi

ios - UIAlertController:添加 subview

转载 作者:可可西里 更新时间:2023-11-01 02:05:48 26 4
gpt4 key购买 nike

我想向我的警报 Controller 添加一个 subview 。但是为什么按钮会在顶部呢?我该如何解决这个问题?

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

enter image description here

最佳答案

UIAlertController 是一个非常封闭的系统。它被设计为系统标准警报。你不应该向它添加 subview 。

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

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

关于ios - UIAlertController:添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43002236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com