Show UIPickerView text field is selected, then hide after selected
If I understood well your question, you want:
- Have an
UITextField
which display a text selected - Opening a picker when the user click on the
UITextField
- Close the picker when an item (in the picker) is selected, and set it in the
UITextField
This is the complete code to manage it, you just have to link the delegate of your UITextField
:
@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()
var bizCat = ["Cat One", "Cat Two", "Cat Three"]
override func viewDidLoad() {
super.viewDidLoad()
pickerBizCat.hidden = true;
textfieldBizCat.text = bizCat[0]
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return bizCat.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return bizCat[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerBizCat.hidden = false
return false
}
What I changed from your code:
- Used
UITextFieldDelegate
to display the picker when theUITextField
is selected - Hide the picker once an item is selected, and setup the
UITextField
- Set the first row of your picker in the
UITextField
when any item is selected
Swift - hide pickerView after value selected
If you want to hide the pickerView, try this:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if activeDataArray == season {
enterSeason.text = season[row] as String
}
else if activeDataArray == sport {
enterSport.text = sport[row] as String
}
//trying to hide the dataPicker
self.view.endEditing(true)
//dataPickerView.reloadAllComponents()
//self.dataPickerView.resignFirstResponder()
//self.dataPickerView.frameForAlignmentRect(CGRectMake(0, 900, 375, 219))
}
How to hide UIPIckerView when the user touches the row already selected
Conform class to UIGestureRecognizerDelegate
protocol
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIGestureRecognizerDelegate {
// ...
}
Add a UITapGestureRecognizer
to your colorPickerView
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
tap.cancelsTouchesInView = false
tap.delegate = self
colorPickerView.addGestureRecognizer(tap)
Implement UIGestureRecognizerDelegate
method
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Finally implement tapAction
func tapAction(_ tapRecognizer:UITapGestureRecognizer) {
if tapRecognizer.state == .ended {
let rowHeight : CGFloat = colorPickerView.rowSize(forComponent: 0).height
let selectedRowFrame: CGRect = colorPickerView.bounds.insetBy(dx: 0.0, dy: (colorPickerView.frame.height - rowHeight) / 2.0)
let userTappedOnSelectedRow = selectedRowFrame.contains(tapRecognizer.location(in: colorPickerView))
if (userTappedOnSelectedRow){
let selectedRow = colorPickerView.selectedRow(inComponent: 0)
//do whatever you want here
}
}
}
Replicate steps 2 and 4 using sizerPickerView
to extend functionality.
Hiding/ Showing UIPickerView
UIPickerView inherits from UIView, so you should be able to just toggle its 'hidden' property:
if (pickerView) pickerView.hidden = !pickerView.hidden;
How to hide a UIPickerView when the user make its choice
I'm using a UIPickerView as the inputView for a TextField (thus replacing the onscreen keyboard). I use the following delegate to dismiss the view.
#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// Code logic
[[self view] endEditing:YES];
}
Related Topics
How to Add a Footer to a Uitableview in Storyboard
Uiwebview Random Crash at [Uiviewanimationstate Release]: Message Sent to Deallocated Instance
Swift - Instantiating a Navigation Controller Without Storyboards in App Delegate
Drop Cap with Nsattributedstring
How to Set an Title of the Currently Playing Audio in iPhone Lock Screen
Can't Install Watchkit App on Apple Watch
Xcode Crash Organizer Does Not Symbolicate .Xccrashpoint Files
Use Uibarbuttonitem Icon in Uibutton
How to Animate a Uibutton Between Two Png Images
iOS Autolayout to Center My View Between Two Views
How to Downscale a Uiimage in iOS by the Data Size
How to Zoom a Uiscrollview Inside of a Uicollectionviewcell
Making Video from Uiimage Array with Different Transition Animations
Error: Module File's Minimum Deployment Target Is iOS8.3 V8.3
How to Reference Uitableviewcontroller from a Uitableviewcell Class
Why Does My Uitableview "Jump" When Inserting or Removing a Row