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.
Related
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
I am using a viewController to handle two ChildViewControllers, each containing a UITableView. Would it be possible to set the the position y of a SubView of viewController (e.g. a UILabel) depending on the scrollView.contentOffset of the current ChildViewController?
It works fine with its own subviews already,..
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.testConstt.constant = scrollView.contentOffset.y
}
Thanks for helping!
Just observe the correct scroll view using a conditional statement. I assume the scroll views of the children are table views, so you may do something like this:
let tableViewA = UITableView(...)
let tableViewB = UITableView(...)
let someScrollView = UIScrollView()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableViewA {
// observe a specific table view's scroll view and do something
} else if scrollView == someScrollView {
// observe a specific scroll view and do something
}
}
Remember, UITableView is a subclass of UIScrollView so they can be treated the same in scrollViewDidScroll(_ scrollView:).
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
}
I have a VC that contains a collectionView and a scrollView. I put this code to change current page of pageController by scrolling in scrollView :
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageController.currentPage = Int(pageNumber)
}
It works nice for scrollView but the problem is when i even scroll in collectionView it declares and causes unwanted changing in pageController!
What should i do?
In addition to the answers posted above, You can make use of the tag property of the view.
Just assign a tag (Int) to your scrollview either in xib or via code.
yourScrollView.tag = 10
And in the scrollview delegate method check for this tag:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.tag == 10 {
///Your scrollview was scrolled
} else {
// Collection view was scrolled
}
}
if scrollView == yourScrollView{
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageController.currentPage = Int(pageNumber)
}
check if the instance running the method is actually your scrollView
First you have to understand bit detail about UICollectionView. The UICollectionView is subclassed from UIScrollView. That's why its getting called for both the scrolling (Scrolling collection view and scrolling scroll view). You can do like this to differentiate what type of scrolling it is,
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == "Your scrollView outlet name" { // Scroll view
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageController.currentPage = Int(pageNumber)
} else {
// Collection view
}
}
Thanks.
You need to check the scrollView inside the scrollViewDidEndDecelerating if it is your scroll view or your collection view.
Just add an if scrollView == myScrollView before doing the 2 lines of code you have.
Your code shoudl look like this
func
scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == myScrollView {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageController.currentPage = Int(pageNumber)
}
}
I have a scrollview inside tableview (tableview's header) and I want to call this action when scroll finish:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(bookScrollView.contentOffset.x / bookScrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
print("scroll")
}
when I scroll bookScrollView nothing happen, but when table view scroll this function run.
How I detect when bookScrollView scroll and run function?
thanks
Here, you can define, scrollOfTable, or you can also define TAG property. identify and only scroll when scrollview comes in, not tableview.
You also need to check, if your scrollView is setting delegate
self.vsScrollView.delegate = self
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView == scrollOfTable {
}
else if scrollView == scrollOfScrollView {
}
else {
//something else
}
}