TLYShyNavBar/README.md

229 lines
12 KiB
Markdown
Raw Normal View History

2015-11-15 11:17:04 +08:00
![TLYShyNavBar banner](http://imgur.com/UVxJLxu.png)<br />
__v1.0, Finally! With better code design, and fully featured!__
-----
2015-11-23 15:07:41 +08:00
![Pod Version](https://img.shields.io/cocoapods/v/TLYShyNavBar.svg)
2015-07-06 07:00:13 +08:00
![Pod License](https://img.shields.io/badge/license-MIT-blue.svg)
2015-11-19 18:17:54 +08:00
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
2015-07-06 06:56:59 +08:00
2015-11-15 11:17:04 +08:00
This component helps you mimic the navigation bar auto scrolling that you see in the Facebook, Instagram, 9gag (which uses this!) and other apps. Not only that, but with the ability to add an additional extension that scrolls along as well! It is designed for **ease of use**, and is battle tested in our own [Telly app](https://itunes.apple.com/us/app/telly/id524552885?mt=8)<sup>[1]</sup>!
2014-06-27 20:33:01 +08:00
2014-06-27 20:53:51 +08:00
![Battle Tested!!](resources/battle-tested-demo.gif)
<sub>[1]: AppStore version doesn't have the latest, though. Coming soon. :grin:</sub><br />
<sub>[*]: Content are shown for demo purpose only of how this component is used in the Telly app. We hold the right to show those contents as part of our contract with Sony Pictures.</sub>
## Outline
2014-06-27 21:46:31 +08:00
+ **[Features](#features)**: See what this component has to offer!
+ **[Quick Start](#quick-start)**: TL;DR people, you'll love this.
2014-08-06 05:27:17 +08:00
+ **[Design Goals](#design-goals)**: The cherished aspects of this component.
2014-06-27 22:21:56 +08:00
+ **[A Deeper Look](#a-deeper-look)**: You're invensted in this now and want to make the most out of it.
2014-06-27 21:46:31 +08:00
+ **[How it Works](#how-it-works)**: The deep stuff...
+ **[Remarks](#remarks)**: Read this before losing all hope.
2015-11-15 11:17:04 +08:00
+ **[Contributing](#contributing)**: Together, making iOS better.
2015-10-09 05:17:58 +08:00
+ **[Author](#author)**: Watashi-da!
2014-06-27 21:46:31 +08:00
+ **[Similar Projects](#similar-projects)**: Similar projects that influenced the project in some way.
2014-06-27 20:33:01 +08:00
2014-06-27 21:46:31 +08:00
## Features
2014-06-23 02:11:08 +08:00
2015-10-11 09:31:49 +08:00
| Feature | Demo |
2015-11-15 11:17:04 +08:00
|---|---|
| Scroll a `UINavigationBar` with an extension view | ![](resources/basic-feature.gif) |
| Support opaque and translucent `UINavigationBar`s | ![](resources/opaque-supported.gif) |
| Fully featured, with animations and variable resistance | ![](resources/fully-featured.gif) |
| Responsive, resilient and robust | ![](resources/robust.gif) |
| Supports `UITableView`, with headers, too! | ![](resources/tableview.gif) |
| `UICollectionView` is more than welcome | ![](resources/collectionView.gif) |
| In-call status bar? No problem! | ![](resources/in-app-call.gif) |
| Sticky extension view (Thanks @yukaliao !) | ![](resources/sticky-extension.gif) |
| Sticky navigation bar (Thanks [@TiagoVeloso](https://github.com/TiagoVeloso)!) | ![](resources/sticky-navbar.gif) |
| Fade the entire navbar (Thanks [__@longsview__](https://github.com/longsview)!) | ![](resources/fade-navbar.gif) |
2015-10-09 05:07:45 +08:00
You can test some of these features in the Objective-C demo:
![](resources/features-testing.png)
2014-06-27 21:46:31 +08:00
## Quick Start
1. Get the component
2015-10-09 05:21:45 +08:00
+ Using [CocoaPods](http://cocoapods.org):<br />
2015-10-11 06:08:52 +08:00
Add the following to you [Podfile](http://guides.cocoapods.org/using/the-podfile.html) `pod 'TLYShyNavBar'`<br />
Import the header `#import <TLYShyNavBar/TLYShyNavBarManager.h>`
2015-11-19 18:20:42 +08:00
+ Using [Carthage](https://github.com/Carthage/Carthage) (Thanks [@bradleyayers](https://github.com/bradleyayers)!):<br />
Add the following to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md) `github "telly/TLYShyNavBar"`<br />
Import the header `#import <TLYShyNavBar/TLYShyNavBar.h>`
2014-06-27 21:46:31 +08:00
2015-10-11 06:08:52 +08:00
+ Using Submodules:<br />
Download the project/git submodules, and drag the `TLYShyNavBar` folder to your project. <br />
Import the header `#import "TLYShyNavBarManager.h"`
2014-06-27 21:46:31 +08:00
2015-10-11 06:09:56 +08:00
2. Write one line of code to get started!!
2015-10-09 05:22:31 +08:00
2014-06-27 21:46:31 +08:00
```objc
/* In your UIViewController viewDidLoad or after creating the scroll view. */
self.shyNavBarManager.scrollView = self.scrollView;
```
2015-11-15 11:17:04 +08:00
**IMPORTANT NOTES!!**
2014-06-27 21:46:31 +08:00
2015-11-15 11:17:04 +08:00
1. Don't use with `UITableViewController`. Add a `UITableView` as a subview of `UIViewController` instead.
2. If you are assigning a delegate to your scrollView, do that **before** assigning the scrollView to the `TLYShyNavBarManager`! To learn more, [see below](#how-it-works).
2015-11-15 11:17:04 +08:00
### Using TLYShyNavBar in Swift
2015-11-15 11:17:04 +08:00
Nothing special is needed, really. Just make sure you have a [Bridging Header](http://stackoverflow.com/questions/31716413/xcode-not-automatically-creating-bridging-header) setup, and import:
2015-11-15 11:17:04 +08:00
```objc
#import "TLYShyNavBarManager.h"
```
2015-11-15 11:17:04 +08:00
Then, you should be able to follow the Objective-C instructions, since the code is almost identical.
2014-08-06 05:27:17 +08:00
## Design Goals
+ **Ease of Use**: This is the most important, and should never be compromised. Even if compatability breaks or versatility is limited, the component should remain easy to integrate.
+ **Portable**: Less dependencies, lightweight, self-contained, ... etc.
+ **Compatability**: Whenever possible, the component should simply work with whatever you throw at it.
2014-06-27 21:46:31 +08:00
## A Deeper Look
2015-11-15 11:17:04 +08:00
The above example, while small, is complete! It makes the navigation bar enriched with humility, that it will start getting out of the way when the scroll view starts scrolling. But, you may want to do more than that!
2014-06-27 21:46:31 +08:00
2014-06-27 22:21:09 +08:00
#### ACCESS THE MANAGER OF SHYNESS
2014-06-27 21:46:31 +08:00
Simply access it within your `UIViewController` subclass as a property. The property is lazy loaded for you, so you don't have to instantiate anything:
```objc
self.shyNavBarManager
```
2014-06-27 22:21:09 +08:00
#### ADDING AN EXTENSION VIEW
2014-06-27 21:46:31 +08:00
You can assign your own extension view, and it will appear right beneath the navigation bar. It will slide beneath the navigation bar, before the navigation bar starts shrinking (contracting). Adding an extension view is as simple as:
```objc
/* Also in your UIViewController subclass */
[self.shyNavBarManager setExtensionView:self.toolbar];
```
To stick the extension view to the top and have it remain visible when the navigation bar has been hidden:
```objc
/* Also in your UIViewController subclass */
[self.shyNavBarManager setStickyExtensionView:YES];
```
2014-06-27 22:21:09 +08:00
#### CONTROLLING THE RESISTANCE
2014-06-27 21:46:31 +08:00
When you starting scrolling up (going down the view) or scrolling down (going up the view), you may want the navigation bar to hold off for a certain amount (tolerance) before changing states. (i.e. if the user scrolls down 10 px, don't immediately start showing the contracted navigation bar, but wait till he scrolls, say, 100 px).
You can control that using the following properties on the `shyNavBarManager`:
```objc
/* Control the resistance when scrolling up/down before the navbar
* expands/contracts again.
*/
@property (nonatomic) CGFloat expansionResistance; // default 200
@property (nonatomic) CGFloat contractionResistance; // default 0
```
2015-11-15 11:17:04 +08:00
#### CONTROLLING THE FADE BEHAVIOR
You can customize the fade behavior of the `UINavigationBar` through this property:
```objc
/* Choose how the navbar fades as it contracts/expands.
* Defaults to FadeSubviews
*/
@property (nonatomic) TLYShyNavBarFade fadeBehavior;
```
2014-06-27 21:46:31 +08:00
## How it Works
2015-10-11 05:18:08 +08:00
OK, I'll admit that I added this section purely to rant about how this project came together, and the decision making process behind it.
2014-06-27 21:46:31 +08:00
2014-06-27 22:21:09 +08:00
#### THE BASICS
2014-06-27 22:19:32 +08:00
2014-06-27 21:46:31 +08:00
At a component-user level, this works by adding a category to `UIViewController` with a `TLYShyNavBarManager` property. The property is lazily loaded, to cut any unnecessary overhead, and lower the barrier of entry. From the property, you can start customizing the `TLYShyNavBarManager` for that view controller.
Now, you may start asking, what about the navigation bar? Well, the navigation bar is accessed from the view controller your using the manager in. Let's break that down...
1. When you access the `shyNavBarManager` for the first time, it is created with the `self` parameter passed to it, effectively binding the `shyNavBarManager` to the `UIViewController`.
2. The `shyNavBarManager` accesses the `UINavigationBar` through the assigned `UIViewController`.
... And that is how the basic setup is done!
2014-06-27 22:21:09 +08:00
#### THE EXTENSION VIEW
2014-06-27 22:19:32 +08:00
When you call `setExtensionView:`, it simply resizes an internal container view, and adds your extension view to it. There is no magic here, just simple, single view extension.
2014-06-27 22:21:09 +08:00
#### CAPTURING SCROLL VIEW EVENTS
2014-06-27 22:19:32 +08:00
This one was a pain... First, the experiments that this project went through included:
+ Observing the contentOffset property
+ Adding self as a `UIGestureRecognizer` target
+ Adding a `UIPanGestureRecognizer` to the scroll view.
+ Make the user implement `UIScrollViewDelegate`, and send us the events.
The above didn't yield the perfect experience we were hoping for, except the last one. It did, however, make for redundant code everywhere, and forced the component user to implement the `UIScrollViewDelegate`. That's when the `NSProxy` happened.
2014-06-27 22:19:32 +08:00
When you assign the `scrollView` property to the TLYShyNavBarManager, we attach a proxy object to the `UIScrollView` as the delegate, and then the original delegate to that proxy. The proxy forwards the events we are interested in to the `TLYShyNavBarManager`, and of course, does everything else normally for the original selector, you won't even notice a thing!
2014-06-27 22:21:09 +08:00
#### THE DRAWER CONCEPT
2014-06-27 22:19:32 +08:00
2015-10-11 05:18:08 +08:00
The way the offsets are applied to the navigation bar and extension view is through an elegant doubly linked list implementation. We set the offset to the first node (navigation bar), and ...
2014-06-27 22:19:32 +08:00
+ If it is contracting:
2015-10-11 05:18:08 +08:00
- We pass the contraction amount to the next node, and it returns a residual amount.
2014-06-27 22:19:32 +08:00
+ If we are expanding:
- We process the offset in the first node, and pass the residual to the next node.
2014-06-27 21:46:31 +08:00
2015-10-21 11:46:14 +08:00
It is a simple concept. Say we dragged down by 100 px while the navbar was contracted. The navigation bar would take 44 px of that to expand, and then pass the residual 56 px to the next node (extension view) to calculate its offset. The same goes for contracting, but it starts from the last node, all the way up to the navigation bar.
2014-06-27 21:46:31 +08:00
2015-10-11 05:18:08 +08:00
We also add a parent relationship for a single purpose: Make the child follow its parent's offset. So, if the parent (e.g. navigation bar) is scrolling away to the top, we make sure the child accommodates the parent's offset in the calculation, so it appears as if the child is a subview of the parent.
*Note:* Even though there might be an illusion that the views are expanding and contracting, it's really just a translation (scrolling) of the views. There might be an advantage to actually resizing the bounds, so the extension view doesn't appear behind the navigation bar, for example, so that approach might be explored in the future.
2014-06-27 21:46:31 +08:00
## Remarks
2015-11-15 11:17:04 +08:00
There are downsides in making this component as easy to use as it is. If you have read the how it works section carefully, you'd realize that trying to configure the the `shyNavBarManager` before it is included in the `UINavigationController` hierarchy, will break the component, since within the component, we cannot find the navigation bar, and an assert is triggered:
2014-06-27 21:46:31 +08:00
```objc
2015-11-15 11:17:04 +08:00
NSAssert(navbar != nil, @"Please make sure the viewController is already attached to a navigation controller.");
2014-06-27 21:46:31 +08:00
```
Of course, that can be avoided by creating your own `TLYShyNavBarManager`, like so:
```objc
TLYShyNavBarManager *shyManager = [TLYShyNavBarManager new];
shyManager.expansionResistance = 777.f;
/* ... sometime after the view controller is added to the hierarchy */
viewController.shyNavBarManager = shyManager;
```
2015-11-15 11:17:04 +08:00
## Contributing
PRs are welcome! It is important to test changes, though. Simply go over the demo, make sure nothing is broken. Please do check both translucent and opaque modes. Once all is good, you're good to go!
If it is a feature or bug, it would be greatly appreciated if a new view is added to the demo project demonstrating the bug/feature.
Thanks for everyone who opened an issue, shot me an email, and submitted a PR. Special thanks to those who submitted code that got checked in! This project was made possible with your help. ([See contributors graph](https://github.com/telly/TLYShyNavBar/graphs/contributors))
2015-10-09 05:17:58 +08:00
## Author
Mazyod ([@Mazyod](http://twitter.com/mazyod))
2014-06-27 20:33:01 +08:00
## Similar Projects
+ [BLKFlexibleHeightBar](https://github.com/bryankeller/BLKFlexibleHeightBar)
2014-06-28 17:15:34 +08:00
+ [AMScrollingNavbar](https://github.com/andreamazz/AMScrollingNavbar)
2014-06-27 20:33:01 +08:00
+ [GTScrollNavigationBar](https://github.com/luugiathuy/GTScrollNavigationBar)
+ [JKAutoShrinkView](https://github.com/fsjack/JKAutoShrinkView)
2014-06-27 22:33:48 +08:00
+ [SherginScrollableNavigationBar](https://github.com/shergin/SherginScrollableNavigationBar)