4 notes &
DYFloatingHeaderView - A Facebook inspired floating header view
Facebook’s recent attempt to “revert” its official iOS client from HTML5 to a native iOS app seems to have been very well received (well, at least by the iOS community, not sure how the HTML folks think). The new app is now much faster and reliable than before, and although this update is primarily an engineering rework, it does come with a few really nice UI/UX improvements, and the floating header in the home view is one of them.
The following is a screenshot that shows how the new header looks like when it shows:

As we can see from the above screenshot, the header contains three buttons: Status, Photo and Check In. The interesting thing with this header is its location changes as the underlying table (an UITableView, or to be specific, a subclass of UIScrollView) scrolls. When the underlying table first shows, the header floats right on top of the table. When user scrolls up and down, the header shows and hides itself in an elegant way, which both makes way for an optimal screen space for viewing table contents when needed, and allows easy access to the buttons that it contains. What a smart design! How can we implement this in Objective-C?
Feeling inspired, I sat down and started to “decode” the header in my head. About one hour later, DYFloatingHeaderView was created.
How to use
Like other open source iOS controls that I created, DYFloatingHeaderView is very simply to integrate:
[Updated Sep. 18th, 2012] As Sun Long pointed out in his comment, there is a flaw in the original design that by setting scrollView.delegate to an instance of DYFloatingHeaderView, the original delegate will no longer be accessed. The new version has this issue fixed, although it needs a few more steps to integrate, as I’ll outline below.
Step 1: Download the source from Github, unzip, copy and include DYFloatingHeaderView.m and DYFloatingHeaderView.h into your project
Step 2: Since technically the header is not a subview of the table view (otherwise it will scroll together with the table, which makes it very hard if not impossible to maintain its own position), we cannot simply create and reference DYFloatingHeaderView within an UITableViewController. Instead, what we need to do is to make the header a sibling of the table. In order to do this, all we need to do is to create a class (if it doesn’t exist yet) that serves as a container view for both the header and the table. e.g. In Xcode, create a new UIViewController subclass HomeViewController, and add the following lines of code in
viewDidLoad:- (void)viewDidLoad { [super viewDidLoad]; // Floating header DYFloatingHeaderView *floatingHeader = [[DYFloatingHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [DYFloatingHeaderView height])]; [self.view addSubview:floatingHeader]; // Table view self.listViewController = [[SampleListViewController alloc] init]; self.listViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); self.listViewController.headerView = floatingHeader; [self.view insertSubview:self.listViewController.view belowSubview:floatingHeader]; }Step 3: In SampleListViewController, declare a DYFloatingHeaderView property, and update scrollView’s insets (this is needed to allow the scrollView to display contents without being blocked by the header):
// In SampleListViewController.h @property(nonatomic, strong) DYFloatingHeaderView *headerView;And then in SampleListViewController.m:
@synthesize headerView = _headerView; - (void)setHeaderView:(DYFloatingHeaderView *)headerView { _headerView = headerView; [_headerView updateScrollViewInsets:(UIScrollView *) self.view]; }Step 4: Still in SampleListViewController, override the following methods defined in UIScrollViewDelegate, and call the corresponding DYFloatingHeaderView method in each:
// In SampleListViewController.m #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.headerView scrollViewDidScroll:scrollView]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.headerView scrollViewWillBeginDragging:scrollView]; } - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { [self.headerView scrollViewWillBeginDecelerating:scrollView]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self.headerView scrollViewDidEndDecelerating:scrollView]; }There you should have it!
DYFloatingHeaderView in action
The following is a very short YouTube video that I made to demonstrate how DYFloatingHeaderView works in action!
As always, you are welcome to leave a comment below if you have any questions or suggestions. I’d love to know if you use it in your own projects. If you like DYFloatingHeaderView or any other open source components that I created, please consider following me on Twitter. :)
Tweet

















