Search code examples
iosswiftuiscrollviewdelegatesuiscrollviewdelegate

Swift UIScrollViewDelegate not triggers scrollViewDidScroll event


I have two UIScrollViews in my UIViewController both of them have different properties such as pagingEnabled, contentSize & contentInset also i need them to behave separately when scrolling so i create two separate custom classes like below.

class NavigationScrollView: UIScrollView{

}

class ContentScrollView: UIScrollView{

}

also delegates

extension NavigationScrollView: UIScrollViewDelegate{
    // this method not triggers
    func scrollViewDidScroll(scrollView: UIScrollView) {

    }
}
extension ContentScrollView: UIScrollViewDelegate{
    // this method not triggers
    func scrollViewDidScroll(scrollView: UIScrollView) {

    }    
}

In main viewController viewDidLoad() i do

navigationScroller = NavigationScrollView(frame: CGRectMake(0.0, 0.0, frameWidth, 40.0))
contentScroller = ContentScrollView(frame: CGRectMake(0.0, 0.0, frameWidth, frameHeight))

Solution

  • how about you assign the delegate this way?

        class NavigationScrollView: UIScrollView{
            override func didMoveToSuperview() {
                super.didMoveToSuperview()
                self.delegate = self
            }
        }
    
        class ContentScrollView: UIScrollView{
            override func didMoveToSuperview() {
                super.didMoveToSuperview()
                self.delegate = self
            }
        }