How to exhibit UIPickerView in UIAlertController

When you desire to describes the picker view in UIAlertController in the center of the screen or you need to show bottom action sheet. It will explain better view in iOS to implement picker view in UIAlertController rather than creating in UIView it also saves your many code to write and complexity too. An example okay

let message = "\n\n\n\n\n\n"
let alert = UIAlertController(title: "Please Select City", message: message, preferredStyle: UIAlertControllerStyle.actionSheet)
alert.isModalInPopover = true
 
let pickerFrame = UIPickerView(frame: CGRect(x: 5, y: 20, width: SCREEN_WIDTH - 20, height: 140)) // CGRectMake(left, top, width, height) - left and top are like margins
pickerFrame.tag = 555
//set the pickers datasource and delegate
pickerFrame.delegate = self
 
//Add the picker to the alert controller
alert.view.addSubview(pickerFrame)
let rightAction = UIAlertAction(title: "OK", style: .default, handler: {
 (alert: UIAlertAction!) -> Void in
 //Perform Action
})
alert.addAction(rightAction)
let delAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alert.addAction(delAction)
self.parent!.present(alert, animated: true, completion: { _ in })

of picker view in an alert controller:-

Last Update: February 13, 2019  

December 21, 2018   42   Nandini R    Swift-iOS    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn