|
| 1 | +# Flexible header |
| 2 | + |
| 3 | +The flexible header component is a container view whose height and vertical offset react to |
| 4 | +UIScrollViewDelegate events. |
| 5 | + |
| 6 | +## Installation with CocoaPods |
| 7 | + |
| 8 | +To add the Flexible Header to your Xcode project using CocoaPods, add the following to your |
| 9 | +`Podfile`: |
| 10 | + |
| 11 | + pod 'material-components-ios-catalog/FlexibleHeader' |
| 12 | + |
| 13 | +Then, run the following command: |
| 14 | + |
| 15 | + $ pod install |
| 16 | + |
| 17 | +## Design considerations |
| 18 | + |
| 19 | +Most view controllers own their own header in a material app. These headers are flexible, provide |
| 20 | +navigation information and actions, and often display high quality photography that complements the |
| 21 | +underlying content. Flexible Header was designed with the these expectations in mind. |
| 22 | + |
| 23 | +This deviates from the typical UIKit convention of having a UINavigationController that owns and |
| 24 | +manages a single UINavigationBar. The benefits of this deviation are: |
| 25 | + |
| 26 | +- It is easier to build custom transitions from one view controller. |
| 27 | +- Questions such as "what happens whe the header is 50pt tall and we push a view controller wanting |
| 28 | + a 20pt tall header?" are no longer part of the discussion. With UINavigationBar — or any shared |
| 29 | + navigation bar for that matter — resolving this leads to difficult architectural trade offs. |
| 30 | + |
| 31 | +### What's inside |
| 32 | + |
| 33 | +TODO(featherless): Discuss the three classes in this component, their relationship to one another, |
| 34 | +and lead from this to the "Integration" section. |
| 35 | + |
| 36 | +## Integration |
| 37 | + |
| 38 | +TODO(featherless): Go over this section with an editing comb. |
| 39 | + |
| 40 | +TODO(featherless): Discuss injection. Compare this to UITableViewController and how it federates |
| 41 | +access to UITableView. |
| 42 | + |
| 43 | + @interface MyViewController () <MDCFlexibleHeaderParentViewController> |
| 44 | + |
| 45 | +This protocol defines a flexible header view property which you will need to synthesize. |
| 46 | + |
| 47 | + @implementation MyViewController |
| 48 | + |
| 49 | + @synthesize headerViewController; |
| 50 | + |
| 51 | +In order to populate the property, call the `addToParent:` method. |
| 52 | + |
| 53 | + - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
| 54 | + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 55 | + if (self) { |
| 56 | + [MDCFlexibleHeaderViewController addToParent:self]; |
| 57 | + ... |
| 58 | + |
| 59 | +Within your viewDidLoad you can now create and initialize any subviews that you'd like to add to |
| 60 | +your header view. |
| 61 | + |
| 62 | + - (void)viewDidLoad { |
| 63 | + [super viewDidLoad]; |
| 64 | + ... |
| 65 | + |
| 66 | + // Create custom views here. |
| 67 | + UIView *myCustomView = [UIView new]; |
| 68 | + myCustomView.frame = self.headerViewController.headerView.bounds; |
| 69 | + myCustomView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
| 70 | + | UIViewAutoresizingFlexibleHeight); |
| 71 | + [self.headerViewController.headerView addSubview:myCustomView]; |
| 72 | + |
| 73 | + [self.headerViewController addFlexibleHeaderViewToParentViewControllerView]; |
| 74 | + } |
| 75 | + |
| 76 | +Note that any views added to the flexible header view should set their autoresizing masks to |
| 77 | +flexible width and height so that they expand/contract along with the header view. |
| 78 | + |
| 79 | +TODO(featherless): Include "manual" example of using the standard UIKit APIs to add the |
| 80 | +view/controller. |
| 81 | + |
| 82 | +### A note on subclasses |
| 83 | + |
| 84 | +A subclass of your view controller may add additional views in their viewDidLoad, potentially |
| 85 | +resulting in the header being covered by the new views. It is the responsibility of the subclass to |
| 86 | +take the z-index into account: |
| 87 | + |
| 88 | +[self.view insertSubview:myCustomView belowSubview:self.headerViewController.headerView]; |
| 89 | + |
| 90 | +### Usage with UINavigationController** |
| 91 | + |
| 92 | +You may use an instance of UINavigationController to push and pop view controllers that are managing |
| 93 | +their own header view controller. UINavigationController does have its own navigation bar, so be |
| 94 | +sure to set `navigationBarHidden` to YES either all the time (if all of your view controllers have |
| 95 | +headers, or on the `viewWillAppear:` method). |
| 96 | + |
| 97 | +Do **not** forget to do this if you support app state restoration, or your app will launch with |
| 98 | +double navigation bars. |
| 99 | + |
| 100 | +## Tracking a scroll view |
| 101 | + |
| 102 | +In most situations you will want the header to track a UIScrollView's scrolling behavior. This |
| 103 | +allows the header to expand, collapse, and shift off-screen. |
| 104 | + |
| 105 | +To track a scroll view please follow these steps: |
| 106 | + |
| 107 | +### Step 1: Set the tracking scroll view |
| 108 | + |
| 109 | +In your viewDidLoad, set the `trackingScrollView` property on the header view: |
| 110 | + |
| 111 | + self.headerViewController.headerView.trackingScrollView = scrollView; |
| 112 | + |
| 113 | +`scrollView` might be a table view, collection view, or a plain UIScrollView. |
| 114 | + |
| 115 | +### Step 2: Forward scroll view delegate events to the header view |
| 116 | + |
| 117 | +There are two ways to forward scroll events. |
| 118 | + |
| 119 | +**Set headerViewController as the delegate** |
| 120 | + |
| 121 | +You may use this approach if you do not need to implement any of the delegate's methods yourself |
| 122 | +**and your scroll view is not a collection view**. |
| 123 | + |
| 124 | + scrollView.delegate = self.headerViewController; |
| 125 | + |
| 126 | +**Forward the UIScrollViewDelegate methods to the header view** |
| 127 | + |
| 128 | +If you need to implement any of the UIScrollViewDelegate methods yourself then you will need to |
| 129 | +manually forward the following methods to the flexible header view. |
| 130 | + |
| 131 | + #pragma mark - UIScrollViewDelegate |
| 132 | + |
| 133 | + - (void)scrollViewDidScroll:(UIScrollView *)scrollView { |
| 134 | + if (scrollView == self.headerViewController.headerView.trackingScrollView) { |
| 135 | + [self.headerViewController.headerView trackingScrollViewDidScroll]; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { |
| 140 | + if (scrollView == self.headerViewController.headerView.trackingScrollView) { |
| 141 | + [self.headerViewController.headerView trackingScrollViewDidEndDecelerating]; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { |
| 146 | + if (scrollView == self.headerViewController.headerView.trackingScrollView) { |
| 147 | + [self.headerViewController.headerView trackingScrollViewDidEndDraggingWillDecelerate:decelerate]; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView |
| 152 | + withVelocity:(CGPoint)velocity |
| 153 | + targetContentOffset:(inout CGPoint *)targetContentOffset { |
| 154 | + if (scrollView == self.headerViewController.headerView.trackingScrollView) { |
| 155 | + [self.headerViewController.headerView trackingScrollViewWillEndDraggingWithVelocity:velocity |
| 156 | + targetContentOffset:targetContentOffset]; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +### Step 3: Implement prefersStatusBarHidden and query the flexible header view controller |
| 161 | + |
| 162 | +In order to affect the status bar's visiblity you must query the header view controller. |
| 163 | + |
| 164 | + - (BOOL)prefersStatusBarHidden { |
| 165 | + return self.headerViewController.prefersStatusBarHidden; |
| 166 | + } |
0 commit comments