I have a tableView and button on the same view (Siblings),
i need to HIDE the button when the tableView is SCROLLING (Up and Down Direction no problem) and SHOW when it is STOPPED SCROLLING
I tried the following code, but it is not working (Correctly)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
After a lot of search i found this article, but it is in Objective C, can anyone help me to convert it in to Swift 3.
Why not simply implement these two delegate methods:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
If you're only looking to show and hide a button or any view while scrolling, this will work. Just be sure to set the scrollview delegate and it should work without issues.
Theres no need to convert that objc code to swift.
https://screencast.com/t/eTq9Nzfsgs6E
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.
}
}
In my app I have two table views. Both are same height and width and same cell count.
I want when tableview A Scroll Simultaneously tableview B also scroll.
You can use scrollview delegate scrollViewDidScroll for this purpose. This will do the exact thing you want.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.tableA{
self.tableB.setContentOffset(self.tableA.contentOffset, animated: false)
}
else if scrollView == self.tableB{
self.tableA.setContentOffset(self.tableA.contentOffset, animated: false)
}
print("scrollViewDidScroll") // to check whether this function is called while scrolling
}
TRY THIS:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
As Tableview inherits from ScrollView. Try this.
FOR SWIFT 4:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
Instead of using tableview you can use UIPickerView with two components.
If you need code let me know.
I have an UIViewController containing a MKMapView and an UITableView. Each cell in the tableView contains a set of coordinates. If you want to see, please check this screenshot
I want to the update MKMapView "in real time" as the tableview is scrolling with contains coordinates.
I don't need to use the didSelectRow method. Because user does not select yet.
What is the right code blocks for scrollViewWillBeginDragging method?
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
}
Thank you
I propose the following as one possibility.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
for visibleIndexPath in self.tableView.indexPathsForVisibleRows! {
print(visibleIndexPath)
// 1. Get coordinates from indexPath
// e.g. restaurants[indexPath.row]
// 2. Add annotation to a map view
}
}
I have a horizontal UIScrollView with paging enabled containing 3 view controllers. The middle view contains touch handling that sometimes gets interpreted as scrolling and runs the user experience. I have navigation buttons for moving to page 1 and 3 but want to preserve the scrolling abilities when in page 1 and 3 since they don't have multitouch/complex touch behaviors.
You can achieve this by implement the delegate method func scrollViewDidScroll(_ scrollView: UIScrollView)
here are some example code in Swift:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x == view.frame.width {
scrollView.isScrollEnabled = false
} else {
scrollView.isScrollEnabled = true
}
}
I am making an iOS app which contains a detailed text under UITextView.I added a share button in the view beside a UITextView. I want the button to be hidden when the user starts scrolling and return back when scrolling is not detected.
What I did was this...
if(detailDescriptionTextView.isScrollEnabled == true)
{my button.isHidden = true }
The above code hides the button entirely since scrollview is on by default. So what shall I do?
As of UITextView inherits from UIScrollView you can use UIScrollViewDelegate method for this purpose. You need to just set the delegate of UITextView and implement below methods of UIScrollViewDelegate and you all set to go.
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if yourTextView = scrollView {
yourButton.isHidden = true
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if yourTextView = scrollView {
yourButton.isHidden = false
}
}
UITextView is a UIScrollView subclass. Therefore the UIScrollView delegate method you are using is also available when using UITextView.
you should use scrollViewDidScroll to detect scroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
button.isHidden = true
}
You can use scrollViewDidEndDecelerating . It will call when textView stop scrolling.
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
button.isHidden = false
}