gpt4 book ai didi

带有 pickerView 按钮 Action 的快速 UIAlertController 熬夜

转载 作者:IT王子 更新时间:2023-10-29 05:22:05 25 4
gpt4 key购买 nike

我是 swift 的新手,我正在尝试使用 PickerView 制作 UIAlertContoller但我的按钮有问题,这里有照片

enter image description here

我正在尝试更改按钮的约束以保持向上。我在这里阅读了很多答案,但我没有找到任何解决方案

这是我的代码:

func distance(){
let editRadiusAlert = UIAlertController(title: "Choose distance", message: "", preferredStyle: UIAlertControllerStyle.alert)
let pickeViewFrame: CGRect = CGRect(x: 0, y: 0, width: 250, height: 300)
let pickerViewRadius: UIPickerView = UIPickerView(frame: pickeViewFrame)
pickerViewRadius.delegate = self
pickerViewRadius.dataSource = self
editRadiusAlert.view.addSubview(pickerViewRadius)
editRadiusAlert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default,handler:nil))
editRadiusAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
editRadiusAlert.view.addConstraint(NSLayoutConstraint(item: editRadiusAlert.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.5))
self.present(editRadiusAlert, animated: true, completion: nil)
}

最佳答案

不要将 pickerView 添加为 subview ,而是尝试像这样设置 UIAlertControllercontentViewController

let vc = UIViewController()
vc.preferredContentSize = CGSize(width: 250,height: 300)
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
pickerView.delegate = self
pickerView.dataSource = self
vc.view.addSubview(pickerView)
let editRadiusAlert = UIAlertController(title: "Choose distance", message: "", preferredStyle: UIAlertControllerStyle.alert)
editRadiusAlert.setValue(vc, forKey: "contentViewController")
editRadiusAlert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
editRadiusAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(editRadiusAlert, animated: true)

它看起来像下面。

enter image description here

关于带有 pickerView 按钮 Action 的快速 UIAlertController 熬夜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40190629/

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