18

I have the following layout in my view controller. I want to be able to scroll vertically with the header scrolling off the view and the UISegmentedControl sticking to the top of the view, beyond that the remaining scroll should be handled by the Collection View.

However I'm a bit confused as to what is the best approach to implemented this layout.

Layout

I tried a few implementations with mixed results:

  1. UIScrollView with UICollectionView as subviews: UIScrollView as the parent view with the header, segmented control and collection views as child controls. The problem with this approach is that the nested scrolling does not seem to work correctly. To be able to scroll the UIScrollView the tap needs to be outside the CollectionView area otherwise only the CollectionView scrolls and the header and segmented control don't move.

  2. Header and Segmented Control in Header cell: I tried another approach by using a single CollectionView. I added the header and Segmented Control as subviews of a single Header cell of the collection view. When the segmented control value was changed, I switch the data source property of the CollectionView to achieve the 3 views required for the collection view. Visually everything works perfectly. The only problem here is the race condition when switching quickly between first,second and third tabs. I load the data from a web service, if the web service takes time and is still loading the data and I quickly switch the tabs then I run into bugs where the data returned is for a different collection view than what is currently selected, a lot of out of order sync issues.

  3. Update constant value for Autolayout Constraint: Another approach I tried is to change the constant value of the auto layout constraint applied to "Header" view. Then I added a gesture to the view controller's view to track the scroll, as the user scrolls vertically I adjust the constant of the auto layout constraint so that the "header" cell pops out of view. Again this doesn't seem to work that smoothly, but I suppose I can tweak it, but it seems sort of a hack.

Is there a better way to implement this layout?

0m3r
  • 12,286
  • 15
  • 35
  • 71
newbie
  • 1,485
  • 2
  • 18
  • 43
  • 1
    Is using AutoLayout a requirement for you? – Christian Schnorr Jun 17 '15 at 12:35
  • 1
    yes, auto layout is required – newbie Jun 17 '15 at 18:58
  • 2
    The possible solution for problem you encounter in your 2nd approach (actually, I think this problem is not the matter of layout you choose) can be found in the first part of "Building Concurrent User Interfaces on iOS" [WWDC 2012](https://developer.apple.com/videos/wwdc/2012/) session. – Anastasia Jun 17 '15 at 23:24

6 Answers6

9

#2 seems like a good solution — the scrolling gestures will be most consistent with what users expect, since it's all a single scroll view. (I agree that #3 sounds like a hack.) You can make the header "sticky" with some custom layout attributes.

The only problem here is the race condition when switching quickly between first, second and third tabs.

This is a common problem with asynchronous loading when views are being switched out (especially when you are loading data into individual cells, which are being reused as you scroll). It is important that upon receiving the data you always check whether the receiver is still expecting it; i.e., you should check the segmented control value before changing the backing data source. You could also:

  • Use separate data source objects for the different segments, having each one manage its own data fetching so they can't get mixed up.
  • Cancel the outstanding requests, if you can, when quickly switching tabs, to avoid unnecessary network requests.
  • Cache data to avoid re-fetching every time you switch tabs.
Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266
2

I think you want the same functionality that pinterest profile page have. To implement such functionality at easy way, you need to do following things.

Step 1 : Add UIView as tableHeaderView those who showing off while scrolling up.

self.tableHeaderView = yourView

Step 2 : Add UISegmentControl in section header view.

 - (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section{
  return your_segmentcontrolView;

}

Step 3 : Add UICollectionView into first row of first section.

By implementing following way, you can got your desire functionality.

Hope this help you.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
  • That sounds possible, i'm not sure if i'll run into the same weird scroll within a scroll mess, basically its the same as my solution#1 instead of scrollview you are suggesting I can use TableView which itself uses scrollview. Have you tried this approach in the past? – newbie Jun 19 '15 at 05:46
  • @newbie, yes i have implemented in one of may app. i was suggested `UITableView` because header of section view its self stickly to top of screen until new section reach to top. so you don't need to play with offset. if you want to play with UIScrollview then event you don't get smooth behavior as UItableView Provided. – Jatin Patel - JP Jun 19 '15 at 06:05
  • 1
    @newbie, if my answer help you then can you please award bounty? – Jatin Patel - JP Jun 19 '15 at 07:25
  • 2
    I'm not a fan of users asking the OP to award the bounty to them just because whilst your answer **may** have helped there could have been a better answer to the question. From my personal point of view I don't think this fully answers the question and if it was me I'd say it needs a lot more description to be awarded the 200 rep. Description such as what `1jbandes` would be the type of description I'd expect to see before bounty was rewarded. – Popeye Jun 22 '15 at 09:56
  • @Popeye, sometimes that wan't need longer explanation to give solution. in this case same things happen. and the questioner know what he want. and my answer just satisfied him.. – Jatin Patel - JP Jun 22 '15 at 10:07
  • 1
    @None There are answers on here though that are so much better then yours because they come with an explanation. But either way you shouldn't be asking them to reward you the bounty you need to leave them to make their own mind up. If that means they reward you then great if not then it doesn't matter just move onto the next question and help the next person. – Popeye Jun 22 '15 at 10:28
2

An alternative approach you could consider:

Use a UITableView to contain your UI

  • Create a UITableView, and set your header as the UITableView's headerView.
  • Use a sectionHeader to contain the segmentedControl.
  • Place your collectionView inside of a single UITableViewCell. Or alternatively, you may be able to use the UITableView's footerView to contain the gridView.

By using the sectionHeader, this should allow the header to scroll out of view, but then the sectionHeader will stick below the navigationBar or top of the contentView until another section comes into view (and in your case you will only have one section.)

Aron C
  • 838
  • 1
  • 10
  • 17
1
  • Add Header View, Body View (Holding Segment View & Collection View) into scroll view.
  • Initially set userInteractionEnabled property to "NO" for collection view.
  • Track the insect of scroll view always.
  • If the y-coordinate of the scrolled insect is more than the height of header view, then set userInteractionEnabled property to "YES" so that thereafter collection view can be scrolled.
  • If user scroll outside the scroll view and try to bring the header view down, i.e Scroll view y-coordinate insect is less than the height of header view, then immediately change the user iteration mode of collection view and allow user to scroll the scroll view till the top.
Mithun Ravindran
  • 2,292
  • 2
  • 14
  • 23
1

Rather than implementing this by hand, you could use a library/cocoapod to set this up for you. This one looks like a pretty good fit: https://github.com/iosengineer/BMFloatingHeaderCollectionViewLayout

Plus, the code is open-source, so you can always modify as needed.

BevTheDev
  • 563
  • 2
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mayank Jain Jun 19 '15 at 05:48
  • Thanks for the input @MayankJain. The link points to a library that does what the question is asking about (as opposed to a tutorial or something), so I will update my answer to make that clear. – BevTheDev Jun 19 '15 at 14:20
-5

All I can say is that you need to subclass UIView and make it a delegate of UIGestureRecognizerDelegate and UICollectionViewDelegate, then in your UIView subclass, do the following, I can't give out anymore information on this because the code, although owned by myself, is proprietary to the point of probably enraging quite a few organizations that I've used this for, so here's the secret sauce:

CGPoint contentOffset = [scrollView contentOffset];
CGFloat newHeight = [_headerView maxHeight] - contentOffset.y;
CGRect frame = [_headerView frame];

if (newHeight > [_headerView maxHeight]) {
    frame.origin.y = contentOffset.y;
    frame.size.height = [_headerView maxHeight];
    [_headerView setFrame:frame];
} else if (newHeight < [_headerView minHeight]) {

    frame.origin.y = contentOffset.y;
    frame.size.height = [_headerView minHeight];
    [_headerView setFrame:frame];
} else {
    frame.origin.y = contentOffset.y;
    frame.size.height = newHeight;
    [_headerView setFrame:frame];
}
if ([_delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
    return [_delegate scrollViewDidScroll:scrollView];
}

You must subclass another UIView that is defined as the header for this custom UiCollectionView. Then, you must declare the UIView custom header view inside the custom subview of the UIView/UICollectionView delegate, and then set the header of that custom subview inside the UICollctionViewdelegate. You should then pull in this compounded subclass of UIView/UIcollectionView into your UIViewController. Oh yes, and in your layoutSubViews, make sure you do the height calculations that are passed through a double layered subclass. So, you will have the following files:

  1. UIVew this is the delegate of UICollectionView and what I mentioned before

  2. UIView this is a UISCrollViewDelegate and this is the header view

  3. UIViewController that pulls in the subclassed UIView in number 1

  4. UIView subclass of number 1 that pulls in number 2 and sets it as its header

In the number 4 part, make sure you do something like this:

- (CGFloat)maxHeight
{
    if (SCREEN_WIDTH == 414)
    {
        return 260;

    }else if (SCREEN_WIDTH == 375)
    {
        return 325;

    }else
    {
        return 290;
    }
}

- (CGFloat)minHeight
{
    if (SCREEN_WIDTH == 414)
    {

        return 90;
    }else if (SCREEN_WIDTH == 375)
    {
        return 325;
    }else
    {
        return 290;
    }
}

This will then pass through to the UIView subclass that is a compounded subclass as I already explained. The idea is to capture the maxHeight of you header in the subclass of this header UIView (number 2 above), and then pass this into the main UIView subclass that intercepts these values in the scrollViewDidScroll.

Last tidbit of information, make sure you set up your layoutSubviews in all methods to intercept scroll events. For example in number 1 above, the layoutsubviews method is this:

- (void)layoutSubviews
{
    CGRect frame = [_headerView frame];
    frame.size.width = [self frame].size.width;
    [_headerView setFrame:frame];
    [super layoutSubviews];
}

This is all I can give you, I wish I could post more, but this should give you an idea of how it's done in production environments for the big time apps you see out in the wild.

One more thing to note. When you start going down the road of intense implementations like this, don't be surprised to learn that, for example, a single view controller in an app that works with methods like I've explained will have anywhere from 30-40 custom subclasses that are either subclasses in their own right or compounded subclasses or subclasses of my own subclasses or my own subclasses. I'm telling you this so you get an idea of how much code is required to get this right, not to scare you, but to let you know that it might take a while to get right, and to not kick yourself in the butt if it takes awhile to make work. Good luck!!

Popeye
  • 11,839
  • 9
  • 58
  • 91
Larry Pickles
  • 4,615
  • 2
  • 19
  • 36