scrollViewDidScroll not getting called (I set the delegate!)











up vote
1
down vote

favorite












Situation:
I am creating an imageGallery which is a viewController that I am presenting modally. I have created it all programatically, I haven't touched the storyboard.
The viewController has a scrollView that contains ImageViews, which each hold an image. I have set scrollView.isPagingEnabled = true and it works as expected, I can scroll through the images fine.



Issue:
I am trying to do some work when the scrollView has scrolled. I have set the delegate (which seems to be the main issue people have had when I searched for an answer) but scrollViewDidScroll never gets called.
I'm completely at a loss for what is happening.I've tried quite a lot of different things, but have now run out of ideas



@objc public class ImageGallery: UIViewController, UIScrollViewDelegate {

// MARK:- VARIABLES

/// An array of imageItems
@objc public var galleryItems = [ImageGalleryItem]()

/// The index of the image the gallery should start at
@objc public var startingIndex: NSNumber?

/// The UIImage to display if there is an error
@objc public var errorImage: UIImage?

/// The UIImage to display if the ImageGalleryItem doesn't have an image
@objc public var defaultImage: UIImage?

var scrollView: UIScrollView!

// MARK:- INITS

/// Initialiser for ImageGallery passing an array of ImageGalleryItems
///
/// - Parameter ImageGalleryItems: an array of ImageGalleryItems
@objc public init() {

self.defaultImage = UIImage(named: "default", in: getResourceBundle(), compatibleWith: nil)!
self.errorImage = UIImage(named: "error", in: getResourceBundle(), compatibleWith: nil)!
super.init(nibName:nil, bundle:nil)
}


@objc public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
}


// MARK:- VIEW LIFECYCLES
override public func viewDidLoad() {

super.viewDidLoad()

configureScrollView()

self.scrollView.delegate = self
configureImageViews()
configurePresentationDefaults()
}


func configureScrollView() {

self.scrollView = UIScrollView(frame: view.frame)

self.scrollView.isPagingEnabled = true
self.scrollView.contentOffset = CGPoint(x: CGFloat(truncating: startingIndex ?? 0) * UIScreen.main.bounds.width, y: 0)

let imagesCount = galleryItems.count
let contentWidth = CGFloat(imagesCount) * UIScreen.main.bounds.width
self.scrollView.contentSize = CGSize(width: contentWidth, height: UIScreen.main.bounds.height)


if let toolbar = configureToolBar() {

view.addSubview(self.scrollView)
view.addSubview(toolbar)
view.bringSubviewToFront(toolbar)
}
}

private func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("delegate")
}


Update



I was missing an !



private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}


Did the trick.










share|improve this question




























    up vote
    1
    down vote

    favorite












    Situation:
    I am creating an imageGallery which is a viewController that I am presenting modally. I have created it all programatically, I haven't touched the storyboard.
    The viewController has a scrollView that contains ImageViews, which each hold an image. I have set scrollView.isPagingEnabled = true and it works as expected, I can scroll through the images fine.



    Issue:
    I am trying to do some work when the scrollView has scrolled. I have set the delegate (which seems to be the main issue people have had when I searched for an answer) but scrollViewDidScroll never gets called.
    I'm completely at a loss for what is happening.I've tried quite a lot of different things, but have now run out of ideas



    @objc public class ImageGallery: UIViewController, UIScrollViewDelegate {

    // MARK:- VARIABLES

    /// An array of imageItems
    @objc public var galleryItems = [ImageGalleryItem]()

    /// The index of the image the gallery should start at
    @objc public var startingIndex: NSNumber?

    /// The UIImage to display if there is an error
    @objc public var errorImage: UIImage?

    /// The UIImage to display if the ImageGalleryItem doesn't have an image
    @objc public var defaultImage: UIImage?

    var scrollView: UIScrollView!

    // MARK:- INITS

    /// Initialiser for ImageGallery passing an array of ImageGalleryItems
    ///
    /// - Parameter ImageGalleryItems: an array of ImageGalleryItems
    @objc public init() {

    self.defaultImage = UIImage(named: "default", in: getResourceBundle(), compatibleWith: nil)!
    self.errorImage = UIImage(named: "error", in: getResourceBundle(), compatibleWith: nil)!
    super.init(nibName:nil, bundle:nil)
    }


    @objc public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder);
    }


    // MARK:- VIEW LIFECYCLES
    override public func viewDidLoad() {

    super.viewDidLoad()

    configureScrollView()

    self.scrollView.delegate = self
    configureImageViews()
    configurePresentationDefaults()
    }


    func configureScrollView() {

    self.scrollView = UIScrollView(frame: view.frame)

    self.scrollView.isPagingEnabled = true
    self.scrollView.contentOffset = CGPoint(x: CGFloat(truncating: startingIndex ?? 0) * UIScreen.main.bounds.width, y: 0)

    let imagesCount = galleryItems.count
    let contentWidth = CGFloat(imagesCount) * UIScreen.main.bounds.width
    self.scrollView.contentSize = CGSize(width: contentWidth, height: UIScreen.main.bounds.height)


    if let toolbar = configureToolBar() {

    view.addSubview(self.scrollView)
    view.addSubview(toolbar)
    view.bringSubviewToFront(toolbar)
    }
    }

    private func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("delegate")
    }


    Update



    I was missing an !



    private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
    print("delegate")
    }


    Did the trick.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Situation:
      I am creating an imageGallery which is a viewController that I am presenting modally. I have created it all programatically, I haven't touched the storyboard.
      The viewController has a scrollView that contains ImageViews, which each hold an image. I have set scrollView.isPagingEnabled = true and it works as expected, I can scroll through the images fine.



      Issue:
      I am trying to do some work when the scrollView has scrolled. I have set the delegate (which seems to be the main issue people have had when I searched for an answer) but scrollViewDidScroll never gets called.
      I'm completely at a loss for what is happening.I've tried quite a lot of different things, but have now run out of ideas



      @objc public class ImageGallery: UIViewController, UIScrollViewDelegate {

      // MARK:- VARIABLES

      /// An array of imageItems
      @objc public var galleryItems = [ImageGalleryItem]()

      /// The index of the image the gallery should start at
      @objc public var startingIndex: NSNumber?

      /// The UIImage to display if there is an error
      @objc public var errorImage: UIImage?

      /// The UIImage to display if the ImageGalleryItem doesn't have an image
      @objc public var defaultImage: UIImage?

      var scrollView: UIScrollView!

      // MARK:- INITS

      /// Initialiser for ImageGallery passing an array of ImageGalleryItems
      ///
      /// - Parameter ImageGalleryItems: an array of ImageGalleryItems
      @objc public init() {

      self.defaultImage = UIImage(named: "default", in: getResourceBundle(), compatibleWith: nil)!
      self.errorImage = UIImage(named: "error", in: getResourceBundle(), compatibleWith: nil)!
      super.init(nibName:nil, bundle:nil)
      }


      @objc public required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder);
      }


      // MARK:- VIEW LIFECYCLES
      override public func viewDidLoad() {

      super.viewDidLoad()

      configureScrollView()

      self.scrollView.delegate = self
      configureImageViews()
      configurePresentationDefaults()
      }


      func configureScrollView() {

      self.scrollView = UIScrollView(frame: view.frame)

      self.scrollView.isPagingEnabled = true
      self.scrollView.contentOffset = CGPoint(x: CGFloat(truncating: startingIndex ?? 0) * UIScreen.main.bounds.width, y: 0)

      let imagesCount = galleryItems.count
      let contentWidth = CGFloat(imagesCount) * UIScreen.main.bounds.width
      self.scrollView.contentSize = CGSize(width: contentWidth, height: UIScreen.main.bounds.height)


      if let toolbar = configureToolBar() {

      view.addSubview(self.scrollView)
      view.addSubview(toolbar)
      view.bringSubviewToFront(toolbar)
      }
      }

      private func scrollViewDidScroll(_ scrollView: UIScrollView) {
      print("delegate")
      }


      Update



      I was missing an !



      private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
      print("delegate")
      }


      Did the trick.










      share|improve this question















      Situation:
      I am creating an imageGallery which is a viewController that I am presenting modally. I have created it all programatically, I haven't touched the storyboard.
      The viewController has a scrollView that contains ImageViews, which each hold an image. I have set scrollView.isPagingEnabled = true and it works as expected, I can scroll through the images fine.



      Issue:
      I am trying to do some work when the scrollView has scrolled. I have set the delegate (which seems to be the main issue people have had when I searched for an answer) but scrollViewDidScroll never gets called.
      I'm completely at a loss for what is happening.I've tried quite a lot of different things, but have now run out of ideas



      @objc public class ImageGallery: UIViewController, UIScrollViewDelegate {

      // MARK:- VARIABLES

      /// An array of imageItems
      @objc public var galleryItems = [ImageGalleryItem]()

      /// The index of the image the gallery should start at
      @objc public var startingIndex: NSNumber?

      /// The UIImage to display if there is an error
      @objc public var errorImage: UIImage?

      /// The UIImage to display if the ImageGalleryItem doesn't have an image
      @objc public var defaultImage: UIImage?

      var scrollView: UIScrollView!

      // MARK:- INITS

      /// Initialiser for ImageGallery passing an array of ImageGalleryItems
      ///
      /// - Parameter ImageGalleryItems: an array of ImageGalleryItems
      @objc public init() {

      self.defaultImage = UIImage(named: "default", in: getResourceBundle(), compatibleWith: nil)!
      self.errorImage = UIImage(named: "error", in: getResourceBundle(), compatibleWith: nil)!
      super.init(nibName:nil, bundle:nil)
      }


      @objc public required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder);
      }


      // MARK:- VIEW LIFECYCLES
      override public func viewDidLoad() {

      super.viewDidLoad()

      configureScrollView()

      self.scrollView.delegate = self
      configureImageViews()
      configurePresentationDefaults()
      }


      func configureScrollView() {

      self.scrollView = UIScrollView(frame: view.frame)

      self.scrollView.isPagingEnabled = true
      self.scrollView.contentOffset = CGPoint(x: CGFloat(truncating: startingIndex ?? 0) * UIScreen.main.bounds.width, y: 0)

      let imagesCount = galleryItems.count
      let contentWidth = CGFloat(imagesCount) * UIScreen.main.bounds.width
      self.scrollView.contentSize = CGSize(width: contentWidth, height: UIScreen.main.bounds.height)


      if let toolbar = configureToolBar() {

      view.addSubview(self.scrollView)
      view.addSubview(toolbar)
      view.bringSubviewToFront(toolbar)
      }
      }

      private func scrollViewDidScroll(_ scrollView: UIScrollView) {
      print("delegate")
      }


      Update



      I was missing an !



      private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
      print("delegate")
      }


      Did the trick.







      ios swift uiscrollview uiscrollviewdelegate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 12:06

























      asked Nov 11 at 11:31









      Gareth Miller

      266




      266
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Remove private keyword



          func scrollViewDidScroll(_ scrollView: UIScrollView)





          share|improve this answer





















          • Yeah, I already tried that, I only added private because xcode was showing a warning.
            – Gareth Miller
            Nov 11 at 12:02




















          up vote
          1
          down vote













          You issue comes from the private access level keyword with the scrollViewDidScroll: delegate method. The UIScrollViewDelegate is an NSObject and as the method is private it can't see the method and so the delegate method is not called. As you class is public, you need to declare the method with a public access level.



          Just try this:



          public func scrollViewDidScroll(_ scrollView: UIScrollView) {
          print("delegate")
          }





          share|improve this answer























          • Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
            – Gareth Miller
            Nov 11 at 12:03










          • I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
            – Yannick Loriot
            Nov 11 at 12:05










          • private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
            – Gareth Miller
            Nov 11 at 12:08












          • Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
            – Yannick Loriot
            Nov 11 at 12:12


















          up vote
          0
          down vote













          I was missing an !



          private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
          print("delegate")
          }


          Solved the issue.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248288%2fscrollviewdidscroll-not-getting-called-i-set-the-delegate%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Remove private keyword



            func scrollViewDidScroll(_ scrollView: UIScrollView)





            share|improve this answer





















            • Yeah, I already tried that, I only added private because xcode was showing a warning.
              – Gareth Miller
              Nov 11 at 12:02

















            up vote
            1
            down vote













            Remove private keyword



            func scrollViewDidScroll(_ scrollView: UIScrollView)





            share|improve this answer





















            • Yeah, I already tried that, I only added private because xcode was showing a warning.
              – Gareth Miller
              Nov 11 at 12:02















            up vote
            1
            down vote










            up vote
            1
            down vote









            Remove private keyword



            func scrollViewDidScroll(_ scrollView: UIScrollView)





            share|improve this answer












            Remove private keyword



            func scrollViewDidScroll(_ scrollView: UIScrollView)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 11:37









            Sh_Khan

            35.8k51125




            35.8k51125












            • Yeah, I already tried that, I only added private because xcode was showing a warning.
              – Gareth Miller
              Nov 11 at 12:02




















            • Yeah, I already tried that, I only added private because xcode was showing a warning.
              – Gareth Miller
              Nov 11 at 12:02


















            Yeah, I already tried that, I only added private because xcode was showing a warning.
            – Gareth Miller
            Nov 11 at 12:02






            Yeah, I already tried that, I only added private because xcode was showing a warning.
            – Gareth Miller
            Nov 11 at 12:02














            up vote
            1
            down vote













            You issue comes from the private access level keyword with the scrollViewDidScroll: delegate method. The UIScrollViewDelegate is an NSObject and as the method is private it can't see the method and so the delegate method is not called. As you class is public, you need to declare the method with a public access level.



            Just try this:



            public func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("delegate")
            }





            share|improve this answer























            • Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
              – Gareth Miller
              Nov 11 at 12:03










            • I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
              – Yannick Loriot
              Nov 11 at 12:05










            • private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
              – Gareth Miller
              Nov 11 at 12:08












            • Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
              – Yannick Loriot
              Nov 11 at 12:12















            up vote
            1
            down vote













            You issue comes from the private access level keyword with the scrollViewDidScroll: delegate method. The UIScrollViewDelegate is an NSObject and as the method is private it can't see the method and so the delegate method is not called. As you class is public, you need to declare the method with a public access level.



            Just try this:



            public func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("delegate")
            }





            share|improve this answer























            • Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
              – Gareth Miller
              Nov 11 at 12:03










            • I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
              – Yannick Loriot
              Nov 11 at 12:05










            • private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
              – Gareth Miller
              Nov 11 at 12:08












            • Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
              – Yannick Loriot
              Nov 11 at 12:12













            up vote
            1
            down vote










            up vote
            1
            down vote









            You issue comes from the private access level keyword with the scrollViewDidScroll: delegate method. The UIScrollViewDelegate is an NSObject and as the method is private it can't see the method and so the delegate method is not called. As you class is public, you need to declare the method with a public access level.



            Just try this:



            public func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("delegate")
            }





            share|improve this answer














            You issue comes from the private access level keyword with the scrollViewDidScroll: delegate method. The UIScrollViewDelegate is an NSObject and as the method is private it can't see the method and so the delegate method is not called. As you class is public, you need to declare the method with a public access level.



            Just try this:



            public func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("delegate")
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 12:06

























            answered Nov 11 at 11:37









            Yannick Loriot

            5,94622545




            5,94622545












            • Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
              – Gareth Miller
              Nov 11 at 12:03










            • I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
              – Yannick Loriot
              Nov 11 at 12:05










            • private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
              – Gareth Miller
              Nov 11 at 12:08












            • Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
              – Yannick Loriot
              Nov 11 at 12:12


















            • Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
              – Gareth Miller
              Nov 11 at 12:03










            • I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
              – Yannick Loriot
              Nov 11 at 12:05










            • private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
              – Gareth Miller
              Nov 11 at 12:08












            • Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
              – Yannick Loriot
              Nov 11 at 12:12
















            Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
            – Gareth Miller
            Nov 11 at 12:03




            Yeah, I already tried that, I only added private because xcode was showing a warning. It didn't change anything
            – Gareth Miller
            Nov 11 at 12:03












            I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
            – Yannick Loriot
            Nov 11 at 12:05




            I didn't notice that your class was public, so you need to declare the delegate method as public too. I've edited the post.
            – Yannick Loriot
            Nov 11 at 12:05












            private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
            – Gareth Miller
            Nov 11 at 12:08






            private func scrollViewDidScroll(_ scrollView: UIScrollView!) fixed the issue
            – Gareth Miller
            Nov 11 at 12:08














            Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
            – Yannick Loriot
            Nov 11 at 12:12




            Which swift version are you using? Because the API signature is without the !. See developer.apple.com/documentation/uikit/uiscrollviewdelegate/…
            – Yannick Loriot
            Nov 11 at 12:12










            up vote
            0
            down vote













            I was missing an !



            private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
            print("delegate")
            }


            Solved the issue.






            share|improve this answer

























              up vote
              0
              down vote













              I was missing an !



              private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
              print("delegate")
              }


              Solved the issue.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I was missing an !



                private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
                print("delegate")
                }


                Solved the issue.






                share|improve this answer












                I was missing an !



                private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
                print("delegate")
                }


                Solved the issue.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 12:10









                Gareth Miller

                266




                266






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248288%2fscrollviewdidscroll-not-getting-called-i-set-the-delegate%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine