swift UIAlertController与pickerView按钮操作熬夜

Sah*_*unu 13 xcode uipickerview swift uialertcontroller swift3

我是新手,我正在尝试UIAlertContoller用,PickerView 但我有问题,巴顿斯,这里有一张照片

在此输入图像描述

当我试图改变约束时按钮保持不动我在这里读了很多答案,但我没有找到任何solotuin

这是我的代码:

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)
} 
Run Code Online (Sandbox Code Playgroud)

Nir*_*v D 70

而不是添加的pickerView如子视图尝试设置contentViewControllerUIAlertController这个样子.

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)
Run Code Online (Sandbox Code Playgroud)

它看起来像下面.

在此输入图像描述

  • 请注意,根据文档,您不应修改 UIAlertController 视图层次结构:https://developer.apple.com/documentation/uikit/uialertcontroller “此类的视图层次结构是私有的,不得修改。” 并且此解决方案使用私有 api (即 setValue(vc, forKey: "contentViewController")) (3认同)