-1

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

Vadim
  • 11
  • 2
  • Where exactly do you call `scrollToFirstTab`? I didn't find it in your snippets. – lazarevzubov Oct 06 '22 at 06:01
  • UIViewController -> func scrollToFirstTab -> UIView -> func scrollToFirstTab -> UIView -> func scrollToFirstTab – Vadim Oct 06 '22 at 06:04
  • I don't get what your comment is supposed to mean, but, once again, your code snippets don't show how and where you call the `scrollToFirstTab` method. – lazarevzubov Oct 06 '22 at 12:03
  • (1) I am seriously don't understand your comment, (2) I didn't ask for call chains, and (3) most importantly, I seriously don't understand rudeness of people who ask for help. – lazarevzubov Oct 07 '22 at 05:42

1 Answers1

0

Content offset is not the correct method, offset is the gap between content and scroll view itself. For more about content offset, check this answer.

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

CGRect.zero to scroll to top.

A. Amini
  • 521
  • 8
  • 15