Swift Scroll down should make Bar Item disappear - swift

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.

Related

how to move profile picture to a navigation bar when scrolling up?

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.

How can I invalidate flow layout with animation like in ios?

I'm making an app in Xcode for Mojave OSX.
I want to make a resize of a collectionview and items position of that.
Currently I call to invalidateLayout() method, but it recalculate and set sizes and positions without animations.
In the GIF you could see what is the current behavior, I need that the last item go to second row, in an animated way.
For this i try to override this methods of NSCollectionViewFlowLayout
open override func prepare(forAnimatedBoundsChange oldBounds: NSRect){
}
open override func finalizeAnimatedBoundsChange() {
}
But these never get called, and I don't know what code I need to animate this transition.
(Moved from question to an answer.)
You could override the viewDidLayout of your viewController (or assign an observer to the frame view) and execute this:
collectionView.animator().performBatchUpdates(nil)
This reloads your collectionviewLayout with an animation.

Image in ScrollView not being scrollable

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!

UICollectionView not calling scrollViewDidScroll - Swift

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.

swift UITextView setContentOffset works on didappear and not on willappear

I'm trying to automatically scroll my textview at the top before the view loads, so that you can't see it actually moving upward...
i'm trying doing so with
var zeroOffset = CGPoint.zeroPoint
textSpace.setContentOffset(zeroOffset, animated: false)
where textSpace is my UITextView.
when i place this code inside viewDidAppear it works, but the problem is that obviously you can see for a second the text scrolling, and it does't work at all when i place it inside viewWillAppear (viewDidLoad neither if can be of help).
I guess has something to do with the CGPoint that needs the view to be there before calculate the actual point but i'm not sure, is there a solution to this? thanks
In the viewController lifecycle, between viewWillAppear and viewDidAppear, there is a call viewDidLayoutSubviews. It is called after the subviews have been laid out. At that point, you will have everything you need for your call to work, but it will still be before the view appears. So override viewWillLayoutSubviews and place your call there.