2016-10-22 23 views
4

我在SWIFT新,我试图让UIAlertContollerPickerView 但我有问题的Buttones, 这里照片与pickerView按钮动作迅速UIAlertController熬夜

enter image description here

当我试图改变约束的按钮熬夜 我在这里读了很多答案,但我没有找到任何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) 
} 

回答

31

而不是增加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) 

它看起来像下面。

enter image description here

+0

非常感谢你! –

+0

我尝试反馈,但我没有声望 –

+0

@SaharVanunu欢迎队友:) –