I have pagination in my tableview and I scroll to bottom close to 5-6 pagination iterations down.
How do I scroll to top of the UITableview.
Solutions that I have tried with SO solutions
Sol 1:
DispatchQueue.main.async {
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
Sol 2:
DispatchQueue.main.async {
self.tableView.setContentOffset(.zero, animated: true)
}
Sol 3:
tableView.scroll(to: .top, animated: true)
Sol 4:
https://stackoverflow.com/a/48018130
whenever I am trying, I have checked the Scrollview contentsize of the tableview
The overall scroll is
tableview.scrollview contentSize = [(414.0, 30662.666544596355)]
when I tap on the button to scroll to top = [(0.0, 28960.0)], gradually it is decreasing, Ideally it should to top and make [(0.0, 0.0)] right ?
How can I achieve it ?
Related
I currently have a login and signup buttons labeled as 1 and 2. And underneath that, I have a collectionView with two cells (one for the login UI and one for register UI). When I click on the signup button(Label 2), I want to move the collectionView to the next cell.
I tried using the scrollToItem, but it does not do anything. Anyone know why?
#objc private func didTapRegisterButton() {
let i = IndexPath(item: 1, section: 0)
self.onboardingCollectionView.scrollToItem(at: i, at: .right, animated: true)
}
Figured it out. My collectionView had isPagingEnabled property as true. The scrollToItem doesn't work if that is set to true.
If you want to scroll between two cells on button click then first you need to uncheck(disable) Scrolling Enabled property and check(enable) Paging Enabled property of collectionView. (With this approach user can't scroll manually with swipe)
On button first action,
if self.currentIndexPath == 0 {
return
} else {
self.currentIndexPath += 1
self.yourCollectionView.scrollToItem(at: IndexPath(row: self.currentVisibleIndexPath, section: 0), at: .centeredHorizontally, animated: true)
}
On button second action,
if self.currentIndexPath == 1 {
self.currentIndexPath -= 1
self.yourCollectionView.scrollToItem(at: IndexPath(row: self.currentVisibleIndexPath, section: 0), at: .centeredHorizontally, animated: true)
} else {
return
}
And declare global variable
var currentIndexPath: Int = 0
I'm trying to replicate an animation found on the Apple App Store seen here. The app icons move from left to right continuously like a carousel and I'm trying to produce the same behaviour for a single UIView. Is anyone able to help with this? Really struggling:
Something I tried but it stops and repeats the animation without continuing from left to right.
UIView.animateKeyframes(withDuration: 5, delay: 0, options: .repeat, animations: {
viewOfImageViews.center.x += container.bounds.width +
}, completion: nil)
Here is one way you can archive this effect:
Add a collection view
Set cell width the same width as your superview
Set your collection view scroll direction to horizontal
Now in the code when the collection is loaded you scroll to the first section
DispatchQueue.main.async {
self.collView.scrollToItem(at: IndexPath(item: 0, section: 1), at: .left, animated: true)
}
Now you can add this code below so when animation is done it updates the cells and scroll again
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
colors.append(colors.removeFirst())
collView.reloadData()
DispatchQueue.main.async {
self.collView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .left, animated: false)
self.collView.scrollToItem(at: IndexPath(item: 0, section: 1), at: .left, animated: true)
}
}
Change colors array to your array name which contains the images
I want to show the bottom cells of my Tableview once the data are load I dont want to scroll every time to the bottom of Tableview to see the result
I have try this function in viewDidLoad but it did not work the page load at the top cells
if myArray.count > 0 {
tableView.scrollToRow(at: IndexPath(item:myArray.count - 1 , section: 0), at: .bottom, animated: false)
}
try use this
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [tableView] in
if myArray.count > 0 {
tableView.scrollToRow(at: IndexPath(item:myArray.count - 1 , section: 0), at: .bottom, animated: false)
}
}
You can leverage the usage of the scrollView's setContentOffset:animated: method
This answer should help you out: How to show last add cell on UITableview whileloading view or reloading tableview?
I have to scroll to the bottom of the tableview. In iOS 10 it is working fine and in iOS 11 it scrolls to half of the tableview and stops. It's not scrolling to the bottom of the tableview.
[tableObj scrollRectToVisible:CGRectMake(0, tableObj.contentSize.height - tableObj.frame.size.height,tableObj.frame.size.width, tableObj.frame.size.height) animated:YES];
How about try this.
let indexPath = IndexPath(row: tableDataList.count - 1, section: 0)
table.scrollToRow(at: indexPath, at: .bottom, animated: true)
using this code to scroll to bottom of collection view. however it scrolls to the second last one only.
private func scrollToBottom() {
let lastSectionIndex = (ChatCollectionView?.numberOfSections())! - 1
let lastItemIndex = (ChatCollectionView?.numberOfItemsInSection(lastSectionIndex))!-1
let indexPath = NSIndexPath(forItem: lastItemIndex, inSection: lastSectionIndex)
ChatCollectionView!.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false)
}
is anyone familiar with a bug like that
Your collection view's frame was under the visible area. You should set its bottom inside visible area.