I have imported UICollectionViewDelege, UICollectionViewDatasource and UIScrollViewDelegate.
I have set my collection views delegate and datasource to self.
But scrollViewDidScroll isnt being called, my scroll view goes horizontal if that makes a difference?
Why is scrollViewDidScroll not being called if collection views use the scrollview inside them?
I was moving form swift 2 to swift 3 and what did the trick is the following:
swift 2:
public func scrollViewDidScroll(scrollView: UIScrollView)
to swift 3:
public func scrollViewDidScroll(_ scrollView: UIScrollView)
So basically the "_" underscore did the trick. What is annoying is that swift compiler didn't complain about it!!!
As for inheritance UICollectionViewDelegate should do the trick.
I had to add information that my controller conforms to UIScrollViewDelegate protocol in order to make it work
I've also encountered this issue. I started off by setting the delegate to self inside of storyboard, at that point scrollViewDidScroll wasn't getting invoked (but functions like collectionView willDisplayCell were, as expected). After some playing around I've noticed that setting delegate programmatically fixed the issue.
Related
I want that the Bar Button Item diappear when I scroll down. My problem is that I don't know how to detect the scroll? I have tried some code but nothing worked. For example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("123")
}
(Don't work, I call the method in viewDidLoad())
Also I don't know where I have to call the method. In viewDidLoad(), viewDidAppear() or somewhere else? I am new in Swift, sorry.
Does anyone know the answer?
The thing is you are calling it from viewDidLoad, that gets called only once when the view is loaded. You need to place this scrollViewDidScroll
function separately, after viewDidLoad for example, but not inside of it.
Also make sure you implemented UIScrollViewDelegate in your file. Then you can add scrollViewDidScroll method to detect when is the view scrolled and print('123') inside of it.
Also make sure to set your scrollView.delegate = self in your viewDidLoad.
I have created this layout using UITableView header and I'm trying to move profile picture when scrolling up and removing full name and Phone number. First screen UI is what I have created and it is made in tableview header and
I want to achieve a functionality of a second screen when scrolling up.
You need add UIScrollViewDelegate to your tableView.
Table view inherit UIScrollView and their UIScrollViewDelegate methods.
So... you need set like this:
Somewhere set delegate:
tableView.delegate = self
(I think you mb already implement this because you need to implement UITableViewDelegate methods)
and implement UIScrollViewDelegate method:
extension YourControllerOrAnotherDelegateClass: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// use scrollView.contentOffset to calculate when you need to update navbar
}
}
inside this method you can get content offset of your scrollView and update nav bar when you needed.
Event based methods of my delegate are not called while others are called. For example, willDisplayCell is called, but shouldHighlightItemAt and shouldSelectItemAt are not called. I overwrote point(inside:...) method inside my custom UICollectionView, and it gets called and super returns true. What could be causing this? I'm on iOS 11.4, Swift 4.1, and Xcode 9.4.1.
I have added a supplementary view to the collection view that spans the entire area of its bounds. This view was intercepting all tap events. Once I set its isUserInteractionEnabled to false I started receiving highlight and selection calls on cells in my delegate.
I was asked to help out in a project with swift and work in xcode on a short timescale, neither which I have worked with before.
The task is getting a picture to be "zoomable". And after some researched I found that putting a UIScrollView on top of the image will suffice. Now, since the image view already exists and is integrated in the code on a level I do not dare triffle with, with some constraits and what not.
I dont want to start the process all over with a new image view and later try to hook it into the code. Mostly because the image view is inside a table cell inside of a table view.
What I have done is:
On the storyboard
Put the image view inside the view on the storyboard.
Assigned the delegate of the scroll view to the table cell.
I couldnt quite figure out how to constraint these so I mostly use 'Add missing constraints'.
In code for the TableViewCell
Inherited the UIScrollViewDelegate
made a new var with #IBOutlet weak var scrollView: UIScrollView!
Since I am in a controller, i cannot override viewDidLoad function, so I implemented that and set minimum and maximumZoomScale to some values.
Implemented a viewForZooming function that returns the UIImageView
I figured out somewhat how I can use the constraints and properties of the ImageView to resize and stuff, but the regardless of what I try to do, I cannot get the "zoom" to work.
Is there any property that the ImageView could have that is messing this up, what should I check for?
After reading and trying to figure out, what finally made it work was this:
The ScrollView was made parent in the Interface Builder to the ImageView
I magically made the constraints to fit the ScrollView aligned to everything around it mostly by Add missing constraints when prompted with constraint errors.
Rightclicked the ScrollView then for the delegate, clicked the + sign and dragged that to the UITableViewCell.
I also did the same for the Referencing Outlet
And code for the UITableViewCell is this:
class ClothesItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
var theImage: UIImage? {
set {
clothinhImageView.image = newValue
} get {
return clothingImageView.image
}
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return clothingImageView
}
}
Might not be perfectly clear, and I couldnt find the exact article that made the work done. But there are plenty that tries to solve this issue in different ways, and somehow this worked. Hopes it helpes somebody!
Actually, a UITableView is a UIScrollView (inherits from that). Now, I made a UITableView subclass and added this line of code to it:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"contentOffset: %#", NSStringFromCGPoint(self.contentOffset));
}
For some reason this is never called when I scroll the table view. But since UITableView has a delegate property on it's own, I assume that it must implement UIScrollViewDelegate protocol and is the delegate for the scroll view itself. Isn't it?
How could I intercept scroll position changes? I want to read them only. Probably I couldn't set them with contentOffset, right?
Probably I couldn't set them with
contentOffset, right?
As UITableView inherits from UIScrollView you can get and set its contentOffset property.
Note also that UITableViewDelegate protocol is defined the following way:
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
That is it conforms to UIScrollViewDelegate protocol as well so your tableView's delegate(not UITableView itself) can implement any UIScrollViewDelegate methods and they should get called fine.
Just implement setContentOffset: and call super after you read the values you want. A UITableView is a UIScrollView so you can scroll it by calling setContentOffset: as well.