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.
ios swift uiscrollview uiscrollviewdelegate
add a comment |
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.
ios swift uiscrollview uiscrollviewdelegate
add a comment |
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.
ios swift uiscrollview uiscrollviewdelegate
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
ios swift uiscrollview uiscrollviewdelegate
edited Nov 11 at 12:06
asked Nov 11 at 11:31
Gareth Miller
266
266
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Remove private keyword
func scrollViewDidScroll(_ scrollView: UIScrollView)
Yeah, I already tried that, I only addedprivate
because xcode was showing a warning.
– Gareth Miller
Nov 11 at 12:02
add a comment |
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")
}
Yeah, I already tried that, I only addedprivate
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 waspublic
, 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
add a comment |
up vote
0
down vote
I was missing an !
private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}
Solved the issue.
add a comment |
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)
Yeah, I already tried that, I only addedprivate
because xcode was showing a warning.
– Gareth Miller
Nov 11 at 12:02
add a comment |
up vote
1
down vote
Remove private keyword
func scrollViewDidScroll(_ scrollView: UIScrollView)
Yeah, I already tried that, I only addedprivate
because xcode was showing a warning.
– Gareth Miller
Nov 11 at 12:02
add a comment |
up vote
1
down vote
up vote
1
down vote
Remove private keyword
func scrollViewDidScroll(_ scrollView: UIScrollView)
Remove private keyword
func scrollViewDidScroll(_ scrollView: UIScrollView)
answered Nov 11 at 11:37
Sh_Khan
36.4k51125
36.4k51125
Yeah, I already tried that, I only addedprivate
because xcode was showing a warning.
– Gareth Miller
Nov 11 at 12:02
add a comment |
Yeah, I already tried that, I only addedprivate
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
add a comment |
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")
}
Yeah, I already tried that, I only addedprivate
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 waspublic
, 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
add a comment |
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")
}
Yeah, I already tried that, I only addedprivate
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 waspublic
, 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
add a comment |
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")
}
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")
}
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 addedprivate
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 waspublic
, 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
add a comment |
Yeah, I already tried that, I only addedprivate
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 waspublic
, 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
add a comment |
up vote
0
down vote
I was missing an !
private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}
Solved the issue.
add a comment |
up vote
0
down vote
I was missing an !
private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}
Solved the issue.
add a comment |
up vote
0
down vote
up vote
0
down vote
I was missing an !
private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}
Solved the issue.
I was missing an !
private func scrollViewDidScroll(_ scrollView: UIScrollView!) {
print("delegate")
}
Solved the issue.
answered Nov 11 at 12:10
Gareth Miller
266
266
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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