Home > Blockchain >  ScrollView scrollToPosition programmaticaly
ScrollView scrollToPosition programmaticaly

Time:10-07

i have a ViewController that containts View and One more View inside of it

Scroll works fine when i do it by my mouse, but if i use method scrollView.setContentOffset nothing happens.

I tried to check if scroll available using scrollView.delegate = works fine

UiViewController

class WishListViewController: UIViewController {

 private lazy var wishListHeaderView: WishListHeaderView = {
        let view = WishListHeaderView()
        view.delegate = self
        
        return view
    }()

}

UiView

class WishListHeaderView: UICollectionReusableView {

private lazy var wishListNavigationView: WishListNavigationView = {
        let view = WishListNavigationView()
        view.delegate = self

        return view
    }()
 }

Current view with scroll, that is not working

private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        
        return scrollView
    }()

private lazy var tabsStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.distribution = .fill
        stackView.axis = .horizontal
        stackView.spacing = 8
        stackView.backgroundColor = PaletteApp.grayBackgroundButton
        
        return stackView
    }()

private func commonInit() {
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().inset(16)
            make.height.equalTo(38)
        }
        
        scrollView.addSubview(tabsStackView)
        tabsStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
   }
.........

Here is method in this view, debagger shows that i am in this method. And after this if i use print i see scrollviewOffset (100, 0)

 func scrollToFirstTab() {
        self.scrollView.setContentOffset(CGPoint(x: 100, y: 0), animated: true)
    }

Where is a problem? Thank you

CodePudding user response:

Content offset is not the correct method, offset is the gap between content and scroll view itself.

You need to use func scrollRectToVisible(CGRect, animated: Bool).

CGRect.zero to scroll to top.

  • Related