带有pickerView按钮动作的swift UIAlertController [英] swift UIAlertController with pickerView button action stay up

查看:24
本文介绍了带有pickerView按钮动作的swift UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 新手,我正在尝试使用 PickerView 制作 UIAlertContoller但我对 Buttones 有问题,来一张照片

I am new in swift and I am trying to make UIAlertContoller with PickerView but I have problems with the Buttones, Here a photo

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

I am trying to change the constraint of the buttons to stay up. I read a lot of answers here but I did not find any solution

这是我的代码:

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

推荐答案

尝试设置 UIAlertControllercontentViewController 而不是添加 pickerView 作为子视图像这样.

Instead of adding pickerView as subview try to set contentViewController of UIAlertController like this.

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)

看起来像下面.

这篇关于带有pickerView按钮动作的swift UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆