Hiding/ Showing UIPickerView

后端 未结 8 586
广开言路
广开言路 2020-12-05 11:09

I Have an a touchesEnded event that checks for when a UITextField is pressed. What I would like it to do is is hide/show a UIPickerView. How can this be done?



        
8条回答
  •  温柔的废话
    2020-12-05 11:23

    In a scrollView based application, showing and hiding UIPickerView could be a tough issue as it is relatively hard to pin it to the bottom of the visible screen rectangle. You can do it easily with the following code.

    let alertController = UIAlertController(title: title, message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: .ActionSheet)
    alertController.view.addSubview(pickerView)
    alertController.addAction(UIAlertAction(title: "OK", style: .Cancel) { action in
    })
    self.presentViewController(alertController, animated: true, completion: nil)
    

    To summarize, you create an UIAlertController with an empty message. In order to make the alertController to have the size of your pickerView, you give the message neccessary number of line breaks. And lastly you add your pickerView as a subview of alertController.view

    You then follow the selected pickerView rows with the UIPickerViewDelegate methods.

提交回复
热议问题