2016-10-22 11 views
4

Я новичок в быстры, и я пытаюсь сделать UIAlertContoller с PickerView , но у меня есть проблемы с Buttones, Вот фотобыстро UIAlertController с помощью кнопки действия pickerView остаться

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 в качестве подставки попытайтесь установить contentViewController из UIAlertController вот так.

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 Добро пожаловать, помогите :) –

 Смежные вопросы

  • Нет связанных вопросов^_^