• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A shrinking (shy) navigation bar that automatically adjusts as a user scrolls, with customizable full and shy heights.

SQTShyNavigationBar

A shrinking (shy) navigation bar that automatically adjusts as a user scrolls, with customizable full and shy heights.

The goal is to be as robust as possible, smoothly handling the trickier edge cases - even if that means a little more integration work for you, the developer.

SQTShyNavigationBar Animation

How To Get Started

Installation

  1. Read this README to understand the functionality and limitations.
  2. Install SQTShyNavigationBar, either via CocoaPods or by adding SQTShyNavigationBar.h and SQTShyNavigationBar.m to your project manually.
  3. Replace your standard UINavigationBar with SQTShyNavigationBar - see below!

Usage

Storyboard

If you've set up a Navigation Controller in your Storyboard, you'll need to specify SQTShyNavigationBar as the custom class for the Navigation Bar in your Navigation Controller scene.

You'll need to import SQTShyNavigationBar.h when you want to access the shyNavigationBar property, as described below.

Programmatically

If you're initializing your Navigation Controller programmatically, you'll need to use the initWithNavigationBarClass:toolbarClass: initializer, passing [SQTShyNavigationBar class] for the *navigationBarClass* parameter.

You'll need to import SQTShyNavigationBar.h when you want to access the shyNavigationBar property, as described below.

Configuration

Assuming you're using a Navigation Controller, the SQTShyNavigationBar for your current child controller can be accessed via the shyNavigationBar property (added via class extension) of your UINavigationController.

Using this property, you can configure the various characteristics of SQTShyNavigationBar:

Heights

The shy height is the height to which the navigation bar shrinks to as the user scrolls the content toward the navigation bar (up). The default value is 20.0 points, which just happens to be the height of the current iOS status bar.

The full height is the height to which the navigation bar expands as the user scrolls the content away from the navigation bar (down). The full height defaults to the frame set at initialization, which is typically 44.0 points. Note that this value does not include the height of the status bar.

These heights should be configured in your initial view controller's viewDidLoad method. Changing these heights before pushing/popping a view controller on the navigation stack will work, but it currently looks terrible!

For example, the following would result in a navigation bar which has a "normal" height of 80 points (100 points total, including the status bar), and shrinks to 40 points (20 of which will be the status bar).

self.navigationController.shyNavigationBar.shyHeight = 44.0f;
self.navigationController.shyNavigationBar.fullHeight = 100.0f;

Passing Scrolling Information

SQTShyNavigationBar uses a hybrid approach to track scrolling, by using both direct notification of scroll events via your UIScrollView delegate and an internal pan gesture recognizer.

If you don't already implement the scrollViewDidScroll: UIScrollViewDelegate protocol method, you'll need to add it to your view controller:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.navigationController.shyNavigationBar scrollViewDidScroll:scrollView];
}

SQTShyNavigationBar will follow the last scroll view passed to it via this method.

Update Block

SQTShyNavigationBar has a block property called updateBlock, which allows you to set a block which will be called when the navigation bar shrinks or grows. This block passes in 3 variables: the visible frame of the navigation bar, the "shy" fraction, and an NSArray of subviews. The shy fraction equals 0.0 at shy height, and 1.0 at full height.

The default implementation of this block performs the commonly-expected "fade out subviews" functionality, where the title and/or buttons in the navigation bar fade out as the bar approaches its shy height.

Other implementations should be possible, but these are left up to the developer. WARNING: This block allows you to muck around with UINavigationBar's subviews - you can probably really mess stuff up (perhaps to the point of app rejection) if you aren't careful!

Snapping

The shouldSnap property, which defaults to YES, defines if the navigation bar should "snap" to the most appropriate size should scrolling stop while the bar is at neither the shy height nor the full height.

Enabled

The shrinking of the navigation bar can be disabled by setting the enabled property to NO.

Navigation

In order to help SQTShyNavigationBar prepare itself appropriately for navigation actions, such as pushing/popping another view controller, you'll need to implement two methods in viewDidAppear: and prepareForSegue:sender: (or viewWillDisappear:).

In order to help SQTShyNavigationBar with the transition the suggested method to use in viewDidAppear: is adjustForSegueInto:scrollView:, into which you should pass the relevant scroll view it should follow.

- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController.shyNavigationBar adjustForSequeInto:animated scrollView:self.scrollView];
}

The demo uses prepareForSegue:sender: to call this method, but viewWillDisappear: should also work.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIView *)sender {
    // Do other stuff to prepare...
    
    [self.navigationController.shyNavigationBar prepareForSegueAway:YES];
}

Limitations & Todo

  • Changing shyHeight and fullHeight while changing view controllers is currently buggy due to the way iOS handles view layout during transitions. I would like to get this working, but I may just utilizes a less painful approach and never implement it in this library.
  • Add methods to set height to a specific value.
  • Add documentation for CocoaDocs.

Thanks

About

Charles Powell

Give me a shout if you're using this in your project!