Fast UIAlertController with pickerView button not working

I am new to swift and I am trying to make UIAlertContoller using PickerView but I have problems with buttons, Here's a photo

enter image description here

when I try to change the restriction, the buttons remain. I read a lot of answers here, but I did not find a single saltot.

Here is my code:

 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) } 
+5
source share
1 answer

Instead of adding pickerView as a subquery, try setting the contentViewController of UIAlertController as follows.

 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) 

He looks lower.

enter image description here

+34
source

Source: https://habr.com/ru/post/1258629/


All Articles