Search code examples
iosswiftuialertviewuipickerviewuialertcontroller

uialertcontroller with uipickerview in swift iOS?


i want to show UIPickerView in UIAlertController. i do it successfully but display of alert view is wrong. below is alert view code.

let alertView = UIAlertController(title: "Select Launguage", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet);
alertView.modalInPopover = true;
alertView.view.addSubview(pickerView)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alertView.addAction(action)
presentViewController(alertView, animated: true, completion: nil)

picker view code is below;

var pickerView = UIPickerView();
self.pickerView.delegate = self
self.pickerView.dataSource = self

enter image description here

"OK" button, title of alert and picker view is overlapped. now please help me how to do it.


Solution

  • Insert new lines via the message parameter to create more vertical space.

    let alertView = UIAlertController(title: "Select Language", message: "\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet);
    

    For a better control of the picker view position create the UIPickerView instance with UIPickerView(frame:) and you have to add the picker view to the subviews of the alert controller.