Scrolling with two UICollectionViews - swift

I have two collectionviews in my ViewController. The first one scrolls horizontally and the second scrolls vertically. The horizontal collectionview is at the top of the screen and the vertical scrolling one is right underneath it.
This is similar to how instagram has its stories on top of the instagram feed.
I used this scrollView function to offset the collectionviews when I scroll but the background of the top collectionview still stays on screen. Can anyone assist me with also having the background move when the bottom collectionview scrolls.
Here is my scrollView code:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if feedCollectionView == scrollView {
var scrollBounds = storiesCollectionView.bounds
scrollBounds.origin.y = feedCollectionView.contentOffset.y
storiesCollectionView.bounds = scrollBounds
}
}
Here is a gif of how it looks right now:
I am looking to have similar functionality as the instagram app when you scroll through the home feed.

I think you should just use one collection with two different layouts for each section

Related

How to have a vertical collectionView on top of a tableView in the same scrollView programmatically?

So I have a vertical collectionView using CompositionalLayout and a tableView.
How can I add the collectionView on top of the tableView?
So that the user would scroll down the collectionView and when reached the bottom of it scroll down the tableView?
I tried to put them both in a stackView, with fillEqually but that would just the screen in two with two individual scrolls.
Why not just define all the view to be a collection view with custom layout? Im sure you can achieve the same ui. You can also try wrapping them in a scroll view but this approach has its own difficulties

Display navigationbar when scrolling down

On the top of the ViewController, I have a image and I hide the navigationbar, for a better visual effect.
If the user scrolls up, there is a zoom on the image. No problem so far.
If the user scrolls down, I want to display the navigation bar with animation (very light to the correct background color of the navbar)
I ve checked here a good tutorial with the new possibilities with Ios8.
In fact, i need to perform the opposite of hidesBarsOnSwipe
So firstly, to hide the navigationbar I need to
self.navigationController?.isNavigationBarHidden = true
And after some search, I think I will need to use UIScrollViewDelegate.
But I have no idea how could i implement it .
Any hint?
What you have to do is to implement the UIScrollViewDelegate and more precisely the scrollViewDidScroll(_:) method (see documentation). This method is called each time the scroll view is scrolled.
So, in this method, you have to check that the user scrolled down and then hide the navigation bar by calling the setNavigationBarHidden(_:animated:) method of your current navigation controller (see documentation)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let defaultOffset = view.safeAreaInsets.top
let offset = scrollView.contentOffset.y + defaultOffset
navigationController?.navigationBar.transform = .init(translationX: 0, y: min(0, -offset))
}
use this function and it scrolls up the navigation bar while scrolling up and whenever you scroll down then the navigation bar appears again..

sending UIScrollView to Background

I'm midway of finishing my app. I noticed that one of my views needs extra vertical space so I need to add a UIScrollView. Yet when I add that scroll view it overlaps everything else in the view. In simple words if I need to get it to work properly I have to delete everything off the view , add the scroll view, and then re-add everything back! Is there anyway to send this scroll view to the background of the view? This is all the code that concerns the scroll view
#IBOutlet var scrollerForPOfFourBar: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
/* non related code here*/
scrollerForPOfFourBar.userInteractionEnabled = true
scrollerForPOfFourBar.self.contentSize = CGSizeMake(320, 400)
}
I'm assuming you're using Interface Builder since you marked your scrollView as an IBOutlet.
On the view list, on the left, the order of the views specifies their Z order, with the views at the bottom being the ones drawn on top (with the largest Z).
To move your scrollView to the back, drag it just underneath of the view, and drop it there.
Keep in mind that you will have to drag all the views you had in your view into the scrollView (the easiest is to do it in the views navigator as well), and you'll have to set up any Auto Layout constraints again, which will be a pain.
Are you just want to move scrollerForPOfFourBar to back? Try this:
self.view.sendSubviewToBack(scrollerForPOfFourBar)

How to find which table is scrolled with multiple UITableViews on UIScrollView

I have around 13 UITableViews on UIScrollview,scrollview scrolls horizontally and shows all uitableviews.
on each scroll I want to scroll to particular point and i did that using the code
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if(position >= 12)
position=-1;
position++;
[self scrollToPosition:position];
}
-(void)scrollToPosition:(int) tempPos{
[scrollView setContentOffset:CGPointMake(635*tempPos+tempPos*5, 0)];
}
Problem is: How can I detect whether I scrolled to left or right ??
as per my code whatever direction of the scroll it'll just increase contentOffset, But I want to achieve left scroll as well as right scroll along with setting particular contentOffset to each scroll.
Also, If i scroll any of my uitableViews vertically, then these delegates will get called and set the contentOffset so that it'll scorll horizontally...! so that I cant see the content of the table I scrolled.
I want to scroll horizontally to show each uitableViews(by setting some contentOffset) also I want to achieve verticle scrolling of each table individually.
and How to find which table is currently scrolling with multiple UITableViews on UIScrollView ??
any problem with my code ?? how to achieve this ??
Thanks,
Ranganatha

iPhone how to implement a "Wide" UITableViewCell?

I'd like to have a UITableView with cells wider than 320 points. The user should be able to scroll sideways to be able to view different parts of a UITableViewCell. Is this kind of behavior possible with a UITableView, or should I go and try to implement a tiling UIScrollView?
I tried wrapping a UITableView within a UIScrollView, and the results are terrible - they compete for the scroll gestures and most of the time the scroll view wins, preventing the table from being traversed vertically.
Any input is appreciated!
Thank you!
Update: I tried the proposed solution and it scrolls properly, but the tableview is still only 320 pixels wide. Is tableView's width linked to the window bounds ?
Wrapping the table view with the scroll view is the right way.
UIScrollView with
Show horizontal scrollers
scrolling enabled
autosize to full screen
Inside that, a UITableView
shows vertical scrollers
scrolling enabled
Then I set the table view's frame, with w, being the calculated width of the table view with all columns, whatever your width, and kTableScrollViewHeight being the fixed height of both the table view and the scroll view, in my case, for example 367 points (screen minus status, navbar and tabbar):
tv.frame = CGRectMake(0, 0, w, kTableScrollViewHeight);
and the scroll view's content size
scrollView.contentSize = CGSizeMake(w, kTableScrollViewHeight);
If you want the scroll-to-top behavior when the user taps the status bar, you might want to set
scrollView.scrollsToTop = NO;
because otherwise the scroll view will take away the tap from the table view.