Show Uipickerview Text Field Is Selected, Then Hide After Selected

Show UIPickerView text field is selected, then hide after selected

If I understood well your question, you want:

  1. Have an UITextField which display a text selected
  2. Opening a picker when the user click on the UITextField
  3. 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 the UITextField 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



Leave a reply



Submit