0Day Forums
Scroll UICollectionView to bottom - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: Scroll UICollectionView to bottom (/Thread-Scroll-UICollectionView-to-bottom)

Pages: 1 2


Scroll UICollectionView to bottom - mastiff234 - 07-18-2023

I would like to scroll the UICollectionView to the bottom so the last item is in the view. I have tried to use scrollToItemAtIndexPath but it does not seem to be working. I want this to happen after I have completed a query with Parse.com

Thanks

var query = PFQuery(className:"Chat")
// query.whereKey("user", equalTo:currentUser)
query.whereKey("rideId", equalTo:currentObjectId)
query.orderByDescending("createdAt")
query.includeKey("user")

query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects {
NSLog("%@", object.objectId)

var testId = object.objectId

println(testId)

self.orderedIdArray.append(testId)

var message = object.objectForKey("message") as String
self.messageString = message
self.messageArray.append(self.messageString)
println(message)

var nameId = object.objectForKey("user") as PFUser
var username = nameId.username as String
self.nameString = username
self.namesArray.append(self.nameString)

println("username: \(username)")

self.collectionView?.reloadData()
}

} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
NSLog("Ordered: %@", self.orderedIdArray)
}


RE: Scroll UICollectionView to bottom - arenicolous150 - 07-18-2023

For **Swift** -

**For Horizontal Scrolling** - .right

**For Vertical Scrolling** - .bottom

override func viewDidLayoutSubviews() {
let section = 0
let lastItemIndex = self.dateCollectionView.numberOfItemsInSection(section) - 1
let indexPath:NSIndexPath = NSIndexPath.init(forItem: lastItemIndex, inSection: section)
self.dateCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .right, animated: false)
}




RE: Scroll UICollectionView to bottom - tell633 - 07-18-2023

I have added the lines below to run once the query is complete.

```
var item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
var lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
self.collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
```

Update to **Swift 5**

```
let item = self.collectionView(self.collectionView, numberOfItemsInSection: 0) - 1
let lastItemIndex = IndexPath(item: item, section: 0)
self.collectionView.scrollToItem(at: lastItemIndex, at: .top, animated: true)
```


RE: Scroll UICollectionView to bottom - heliornis443882 - 07-18-2023

**Swift 5** if with animation horizontally

```
func scrollToIndex(index:Int) {
self.collectionView?.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
}

```


RE: Scroll UICollectionView to bottom - Clementia318778 - 07-18-2023

Here's my take in **Swift5**:

```
extension UICollectionView {

public func scrollToLastItem(at scrollPosition: UICollectionView.ScrollPosition, adjustment: CGFloat = 0.0, withAdjustmentDuration duration: TimeInterval = 0.5) {
let lastSection = self.numberOfSections - 1
let lastRowInLastSection = self.numberOfItems(inSection: lastSection)
if lastSection > 0, lastRowInLastSection > 0 {
let indexPath = IndexPath(row: lastRowInLastSection - 1, section: lastSection)
let visibleIndexPaths = self.indexPathsForVisibleItems
if !visibleIndexPaths.contains(indexPath) {
self.self.scrollToItem(at: indexPath, at: scrollPosition, animated: true)
UIView.animate(withDuration: duration) {
switch scrollPosition {
case .top, .bottom, .centeredVertically:
self.contentOffset.y += adjustment
case .left, .right, .centeredHorizontally:
self.contentOffset.x += adjustment
default:
print("Inavlid scrollPosition: \(scrollPosition)")
}
}
}
}
}
}
```


RE: Scroll UICollectionView to bottom - uswrsdofeskjqbnd - 07-19-2023

Swift 5 & 4 Just Call this Function when you want to display the last cell of collectionview

//MARK:- Collection view Scroll to End
func funColvScrollToEnd()
{
let item = self.collectionView(self.colv!, numberOfItemsInSection: 0) - 1
let lastItemIndex = NSIndexPath(item: item, section: 0)
self.colv?.scrollToItem(at: lastItemIndex as IndexPath, at: .bottom, animated: false)
}
//MARK:- You can use .right/ .left/ .top/ .bottom


RE: Scroll UICollectionView to bottom - leukocytic215507 - 07-19-2023

Swift 4.2

let lastSection = self.messagesCollectionView.numberOfSections - 1
let lastRow = self.messagesCollectionView.numberOfItems(inSection: lastSection)
let indexPath = IndexPath(row: lastRow - 1, section: lastSection)
self.messagesCollectionView.scrollToItem(at: indexPath, at: UICollectionView.ScrollPosition.top, animated: true)


RE: Scroll UICollectionView to bottom - epoch518 - 07-19-2023

Swift 4:

For Horizontal Scrolling - `.right`

For Vertical Scrolling - `.bottom`

<!-- begin snippet: hide: false console: true babel: false -->

<!-- language: lang-swift -->

let lastItemIndex = self.collectionView.numberOfItems(inSection: 0) - 1
let indexPath:IndexPath = IndexPath(item: lastItemIndex, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .right, animated: false)

<!-- end snippet -->




RE: Scroll UICollectionView to bottom - surjections17441 - 07-19-2023

**Swift 4**

collectionView?.scrollToItem(at: IndexPath(item: 3, section: 0), at: UICollectionViewScrollPosition.top, animated: false)



RE: Scroll UICollectionView to bottom - answerable167441 - 07-19-2023

You can track top and bottom of your visible view and load next page of items

func scrollViewDidScroll(_ scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
print("bottom")
}

if (scrollView.contentOffset.y <= 0) {
print("top")
}
}