1

I want my scroll view (which is in top half of view) to be scrolled to the bottom of the scroll view when my app is booted up. I want it so that when the app boots up I cannot scroll down any further.

I have tried:

scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 667)
scrollView.contentOffset = CGPoint(x:0, y:self.view.frame.size.height )

However, this only brings my scroll view almost to the bottom. I can still scroll down a little, which I don't want.

I have also tried:

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)

but that doesn't seem to work either.

Ken
  • 1,155
  • 2
  • 19
  • 36
  • 1
    Possible duplicate of [UIScrollView scroll to bottom programmatically](https://stackoverflow.com/questions/952412/uiscrollview-scroll-to-bottom-programmatically) – Renzo Tissoni May 18 '19 at 05:02
  • If you read it, you'll see it's not a duplicate, because OP tried the method you linked and it didn't work. –  Jun 01 '22 at 10:14

3 Answers3

1

You can try below code for moving scroll view to the bottom of the screen:

extension UIScrollView {

    func scrollToBottom() {
        let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
        setContentOffset(bottomOffset, animated: true)
    }
}

Add this extension and try below code to access this extension for scroll to the bottom:

    scrlView.scrollToBottom()

If it is not working properly then you can also try to execute in mail queue:

DispatchQueue.main.async {
   scrlView.scrollToBottom()
}

Let me know if you face any problem.

Hardik Halani
  • 290
  • 2
  • 12
0

You can use this extension if you like :

extension UIScrollView { func scrollToBottom(animated: Bool) { if self.contentSize.height < self.bounds.size.height { return } let bottomOffset = CGPoint(x: 0, y: self.contentSize.height - self.bounds.size.height) self.setContentOffset(bottomOffset, animated: animated) } }

But i would suggest you to not use this as the code is little harder to maintain . What i would suggest is that use a TableView or Collection View inside your View Controller and do the following :

  1. Invert your TableView or CollectionView using the below code . This will make the top the TableView or CollectionView go to bottom but the cell will be inversed:

yourTableView.transform = CGAffineTransform(scaleX: 1, y: -1)

  1. Then in invert your cells using the same code.

cell.transform = CGAffineTransform(scaleX: 1, y: -1)

So by default properties of ios , the tableview should begin from the top . Since we transformed it to show up in bottom of view , the content will be there.

SwiftNinja95
  • 157
  • 2
  • 16
0

Here is the NSScrollView equivalent to the UIScrollView Extension above.

extension NSScrollView {

    func scrollToBottom() {
        let variableHeight =  self.contentView.documentRect.height
        let fixedHeight = self.contentSize.height
        
        if (variableHeight > fixedHeight) {
            self.contentView.scroll(to: CGPoint(x: 0, y: variableHeight))
        }
        
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Reefwing
  • 2,242
  • 1
  • 22
  • 23