【问题标题】:swift UIAlertController with pickerView button action stay up带有pickerView按钮动作的swift UIAlertController
【发布时间】:2017-03-04 14:10:16
【问题描述】:

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

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

这是我的代码:

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

【问题讨论】:

    标签: swift xcode swift3 uipickerview uialertcontroller


    【解决方案1】:

    不要将pickerView 添加为子视图,而是尝试像这样设置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)
    

    如下所示。

    【讨论】:

    • 我尝试反馈,但我没有声誉
    • @SaharVanunu 欢迎朋友 :)
    • 这可能在iOS11 sdk中被破坏了。
    • 请注意,根据文档,您不应修改 UIAlertController 视图层次结构:developer.apple.com/documentation/uikit/uialertcontroller“此类的视图层次结构是私有的,不得修改。”并且这个解决方案使用私有api(即setValue(vc,forKey:“contentViewController”))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多