LuminiCode
LuminiCode

Reputation: 533

Swift: Programmatically added ScrollView - Get the current scroll position of this ScrollView

I have this snippet to add a ScrollView to the main view. How can I get the current scroll position to move a header for example? Does anyone know a solution?

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView!

    let textData: String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud [...]"

    override func viewDidLoad() {
        super.viewDidLoad()

        let textLable = UILabel(frame: CGRect(x: 0, y: 0, width: 1000, height: 200))
        textLable.text = textData

        self.scrollView = UIScrollView()
        self.scrollView.delegate = self
        self.scrollView.contentSize = CGSize(width: textLable.frame.width, height: textLable.frame.height)

        scrollView.addSubview(textLable)
        view.addSubview(scrollView)

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        scrollView.frame = view.bounds
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Upvotes: 0

Views: 677

Answers (2)

LuminiCode
LuminiCode

Reputation: 533

Thank's @ingconti attached my solution for vertical scrolling:


class ViewController: UINavigationController {
    
    var scrollViewer: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let textData: String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud [...]"
        
        let textLable = UILabel(frame: CGRect(x: 0, y: 0, width: 1000, height: 200))
        textLable.text = textData
        
        self.scrollViewer = UIScrollView()
        self.scrollViewer.delegate = self
        self.scrollViewer.contentSize = CGSize(width: textLable.frame.width, height: textLable.frame.height)
        
        scrollViewer.addSubview(textLable)
        view.addSubview(scrollViewer)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        scrollViewer.frame = view.bounds
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset.x
        print(offset)
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    }
}

Upvotes: 0

ingconti
ingconti

Reputation: 11646

A bit unclear the question... if about how much it scrolled...

  1. make your controller obey to UIScrollViewDelegate

  2. add the following method:

func scrollViewDidScroll(_ scrollView: UIScrollView)

  1. ask to scrollview its offset:
  • func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let offset = scrollView.contentOffset.y if offset{ // Your logic. } }

Upvotes: 1

Related Questions