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

标签 swift xcode swift3 uipickerview uialertcontroller

我是 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/

相关文章:

Swift 相当于 C gmtime?

ios - 从 objective-c 调用 swift 函数会出现链接器错误

ios - 如何在 Swift 中创建一组委托(delegate)协议(protocol)项?

ios - 我想念在 firebase 中解析 JSON 的任何内容吗

ios - 如何使用 Xcode Storyboard 保持导入图像的原始尺寸?

ios - 如何去除两个 UITableView 之间的空白?

ios - Xcode 7 中的特定 UI 测试

swift/解析 : how to get most recent item that has data filled in?

swift3 - 使用 Swift 包管理器时仅测试依赖项

swift - 从保存在表格 View 中的已发送电子邮件中获取电子邮件和时间戳