I have created a custom UITableViewCell, and a UITextView was put inside this prototype cell. When I click on this textView, keyboard can bounce automatically. However, if I want to hide the keyboard, I will have to click Return key. It isn't quite convenient.
Here's the question. How can I resign the textView's firstResponder for hiding the keyboard when I scroll the tableview.
Add UITableViewDelegate and add the below method:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
view.endEditing(true)
}
Related
I add this code to enable scroll to dismiss keyboard
tableView.keyboardDismissMode = .onDrag
But the keyboard always lose the animation when it was about to exit,
like at this position
Reference Image
iOS version is 14.4
You can dismiss your keyboard when you scroll with simply using UIScrollViewDelegate method scrollViewDidScroll like below:
extension yourClass: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.searchbar.resignFirstResponder() // Your textField or searchBar etc.
}
}
Below view is a tableViewController which is implemented and set the delegate and datasource methods and it works perfectly fine but I have no idea how to implement the purple view behavior on scrolling up and down.
How can I implement it?
The below code is the way I detect that view scrolled up or down. but still, no clue to implement this behavior.
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
if translation.y > 0 {
presenter?.viewScrolledDown()
} else {
presenter?.viewScrolledUp()
}
}
this question covers how to animate a backgroundColor. If the purple view is a UIView, this won't be a problem. It starts behind the tableView and then moves to the front of the tableView. You can handle this by using these functions:
view.bringSubviewToFront(purpleView)
view.sendSubviewToBack(tableView)
You will also need to animate the autoLayout constraints to move the bottom of the purple view up to the top of the tableView.
I have a scrollview in a ViewController. The scrollview has a UIScrollViewDelegate.On upon that i am adding a Tableview like below:
self.view.addSubview(self.tableView)
But when i am scrolling on tableview cells somehow the scrollview delegate is getting event, which is unexpected.
What can i do to prevent that?
The UITableView is the subclass of UIScrollView and that is the reason you are getting calls to the UIScrollViewDelegate when you are scrolling the cells.
You can not prevent this if you want to have the scrolling enabled for the TableView. I would suggest you check this condition inside your delegate method, for example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView != self.tableView else {return}
}
That's normal because UITableView is a subclass of UIScrollView. Thus you should check if scrollView != self.tableView then do what you want to do inside the scrollView delegate method.
I have a custom view inside a UIScrollView
Tapping/touching works perfectly if UIScrollView is not scrolling.
However, when it is scrolling, I need to tap the scrollview first to stop the scrolling, then tap again the scrollview to select my custom view.
Upon searching, I ended up with scrollViewDidEndDragging
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(!decelerate){
print(scrollView.panGestureRecognizer.location(in: scrollView))
//tap scrollview again but now programmatically
}
}
I wanted to tap the scrollview again with the location(CGPoint) where the last tap location is, but couldn't find any way to do it.
But, if you have any idea on touching my custom view inside UIScrollView while scrolling, that would be really great.
Thank you!
I have a UITableView added to UIViewController. Behind that TableView there is UIImageView like this:
The default behavior of a UITableView is to show whitespace when scrolling up and there is no more content to show. I would like to override this and switch to custom UIPanGestureRecognizer that will move the whole tableview down.
To clarify: When the tableview reaches it top I would like to move the entire tableview instead.
I have set up my PanGestureRecognizer and added it to the tableview and tried this:
func scrollViewDidScroll(scrollView: UIScrollView) {
if self.tableView.contentOffset.y == 0{ //tableview reaches its top
panGestureRecognizer.enabled = true
}else{
panGestureRecognizer.enabled = false
}
}
This does not work at all since the the scrollViewDidScroll triggers at the end of the gesture.
Any other ideas on how I can implement this?