Unable to scroll in uipicker view in iOS?


Unable to scroll in uipicker view in iOS?



I have created a XIB file for picker view & now on tap gesture i am adding the picker view on superview. It is added successfuly but I am not able to scroll.I have also added two button for cancel & done but none of the action for those buttons are detected. Kindly let me know what is the issue



Here is code for adding uipicker view.


@objc func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {


let frame:CGRect = CGRect(x: 0, y: self.view.frame.height + 200, width: self.view.frame.width, height: 200)
self.itemPicker = ItemPicker.instanceFromNib(with: frame) as? ItemPicker
self.itemPicker?.configureView(frame: frame)
self.view.addSubview(self.itemPicker!)
self.itemPicker?.itemPickerDelegate = self
showView()
}

func showView() {
UIView.animate(withDuration: 0.2) {

self.itemPicker?.frame = CGRect(x: 0, y: self.view.frame.height - 200, width: self.view.frame.width, height: 200)
}
}

func hideView() {
UIView.animate(withDuration: 0.2) {
self.itemPicker?.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: 200)

}
}



Code for class picker view


protocol ItemPickerDelegate {

func pickerCancelled()
func pickerDone()
func itemPicked(with item:String)

}

class ItemPicker: UIPickerView {

/// IBOutlets
@IBOutlet weak var pickerView: UIPickerView!
var itemPickerDelegate:ItemPickerDelegate!
var arrItems:[String] = ["Item1","Item2","Item3","Item5","Item6"]

class func instanceFromNib(with frame:CGRect) -> UIView {
let view = Bundle.main.loadNibNamed(Titles.XIB.itemPicker, owner: self, options: nil)! [0] as! UIView
return view
}

func configureView(frame:CGRect) {
self.frame = frame
self.pickerView.delegate = self
self.pickerView.dataSource = self
}

//MARK:IBActions
@IBAction func actionCancel(_ sender: Any) {
DILog.print(items: "actionCancel")
self.itemPickerDelegate.pickerCancelled()
}

@IBAction func actionDone(_ sender: Any) {
DILog.print(items: "actionDone")
self.itemPickerDelegate.pickerDone()
}
}

extension ItemPicker:UIPickerViewDelegate {


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

return arrItems[row]
}
}


extension ItemPicker:UIPickerViewDataSource {

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

return arrItems.count

}

}



Kindly help me out with this problem.





Is it showing all items in pickerView without scrolling ?
– Pravin Tate
Jun 29 at 7:02





Due to UITapGestureRecognizer on main view you are unable to get touch over UIPickerView, to resolve this problem you can use UIGestureRecognizerDelegate and its delegate method , func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } , in this method you can check which UIView is getting interaction with touch .
– Ankit Kushwah
Jun 29 at 7:06






you may use like >>>>>>>>>. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if gestureRecognizer is UITapGestureRecognizer { if touch.view!.isDescendant(of: CollectionView) { return false } } return true }
– Ankit Kushwah
Jun 29 at 7:12





Yes @ Pravin Tate
– Techiee
Jun 29 at 7:18





Now I am showing picker view on textfield begin editing. Kindly make it more clear
– Techiee
Jun 29 at 7:20





1 Answer
1



Have you tried


view.bringSubview(toFront: yourView)





This does not work. I have tried it mania times
– Techiee
Jun 29 at 9:35





okay, Try changing your XIB Superclass from UIPickerView to UIView, if you are adding manually controls in xib then XIB superClass should be UIVIew.
– Akash Soneji
Jun 29 at 9:46






It's already UIView
– Techiee
Jun 29 at 9:52





in your above code it is UIPickerView. correct it from XIB inspector and also from Class File if it is there it should work.
– Akash Soneji
Jun 29 at 9:53






I am using custom class for xib
– Techiee
Jun 29 at 9:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift