Giter Site home page Giter Site logo

amscrollingnavbar's Introduction

CocoaPods Build status Carthage compatible Swift 5 Join the chat at https://gitter.im/andreamazz/AMScrollingNavbar Donate

A custom UINavigationController that enables the scrolling of the navigation bar alongside the scrolling of an observed content view

Versioning notes

  • Version 2.x is written as a subclass of UINavigationController, in Swift.
  • Version 2.0.0 introduced Swift 2.0 syntax.
  • Version 3.0.0 introduced Swift 3.0 syntax.
  • Version 4.0.0 introduced Swift 4.0 syntax.
  • Version 5.1.0 introduced Swift 4.2 syntax.

If you are looking for the category implementation in Objective-C, make sure to checkout version 1.x and prior, although the 2.x is recomended.

Screenshot

Setup with CocoaPods

pod 'AMScrollingNavbar'

use_frameworks!

Setup with Carthage

github "andreamazz/AMScrollingNavbar"

Usage

Make sure to use ScrollingNavigationController instead of the standard UINavigationController. Either set the class of your UINavigationController in your storyboard, or create programmatically a ScrollingNavigationController instance in your code.

Use followScrollView(_: delay:) to start following the scrolling of a scrollable view (e.g.: a UIScrollView or UITableView).

Swift

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let navigationController = navigationController as? ScrollingNavigationController {
        navigationController.followScrollView(tableView, delay: 50.0)
    }
}

Objective-C

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:0 scrollSpeedFactor:1 collapseDirection:NavigationBarCollapseDirectionScrollDown followers:nil];
}

Use stopFollowingScrollview() to stop the behaviour. Remember to call this function on disappear:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    if let navigationController = navigationController as? ScrollingNavigationController {
        navigationController.stopFollowingScrollView()
    }
}

ScrollingNavigationViewController

To DRY things up you can let your view controller subclass ScrollingNavigationViewController, which provides the base setup implementation. You will just need to call followScrollView(_: delay:):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let navigationController = navigationController as? ScrollingNavigationController {
        navigationController.followScrollView(tableView, delay: 50.0)
    }
}

Followers

To move another view, like a toolbar, alongside the navigation bar you can provide the view or multiple views as the followers parameter. Since you might want to have the follower up or down, you'll have to specify the scroll direction of the view once it starts to follow the navigation bar:

if let navigationController = navigationController as? ScrollingNavigationController {
    navigationController.followScrollView(tableView, delay: 50.0, followers: [NavigationBarFollower(view: customFooter, direction: .scrollDown)])
}

Note that when navigating away from the controller the followers might keep the scroll offset. Refer to Handling navigation for proper setup.

Additional scroll

If you want to furhter scroll the navigation bar out of the way, you can use the optional parameter additionalOffset in the followScrollView call.

Scrolling the TabBar

You can also pass a UITabBar in the followers array:

if let navigationController = navigationController as? ScrollingNavigationController {
    navigationController.followScrollView(tableView, delay: 50.0, followers: [tabBarController.tabBar])
}

ScrollingNavigationControllerDelegate

You can set a delegate to receive a call when the state of the navigation bar changes:

if let navigationController = navigationController as? ScrollingNavigationController {
    navigationController.scrollingNavbarDelegate = self
}

Delegate function:

func scrollingNavigationController(_ controller: ScrollingNavigationController, didChangeState state: NavigationBarState) {
    switch state {
    case .collapsed:
        print("navbar collapsed")
    case .expanded:
        print("navbar expanded")
    case .scrolling:
        print("navbar is moving")
    }
}

Handling navigation

If the view controller with the scroll view pushes new controllers, you should call showNavbar(animated:) in your viewWillDisappear(animated:):

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if let navigationController = navigationController as? ScrollingNavigationController {
      navigationController.showNavbar(animated: true)
    }
}

Scrolling to top

When the user taps the status bar, by default a scrollable view scrolls to the top of its content. If you want to also show the navigation bar, make sure to include this in your controller:

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
    if let navigationController = navigationController as? ScrollingNavigationController {
        navigationController.showNavbar(animated: true, scrollToTop: true)
    }
    return true
}

Scroll speed

You can control the speed of the scrolling using the scrollSpeedFactor optional parameter:

controller.followScrollView(view, delay: 0, scrollSpeedFactor: 2)

Check out the sample project for more details.

Changing UINavigationBar.tintColor

AMScrollingNavBar maintains its own copy of the UINavigationBar's tintColor property. You need to notify the AMScrollingNavBar of a tint change by calling navBarTintUpdated():

navigationBar.tintColor = UIColor.red
controller.navBarTintUpdated()

Check out the sample project for more details.

Author

Andrea Mazzini. I'm available for freelance work, feel free to contact me.

Want to support the development of these free libraries? Buy me a coffee โ˜•๏ธ via Paypal.

Contributors

Syo Ikeda and everyone kind enough to submit a pull request.

MIT License

The MIT License (MIT)

Copyright (c) 2014-2019 Andrea Mazzini

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

amscrollingnavbar's People

Contributors

abeansits avatar abohlsen avatar alexookah avatar allewun avatar andreamazz avatar apiyron avatar bobek-balinek avatar bryant1410 avatar dabeck avatar erikackermann avatar feef avatar genadyo avatar gitter-badger avatar hartlco avatar igor-palaguta avatar ikesyo avatar jeffreyjackson avatar jwolkovitz avatar m1entus avatar manuege avatar mark-dlc avatar mrdaios avatar oea avatar p-kislitskiy avatar readmecritic avatar romainbiard avatar rubencodes avatar tifroz avatar xzenon avatar zzz6519003 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amscrollingnavbar's Issues

Update cocoa pod file

Can you update the specs file so that it'll point to the latest version with the fixes. Currently it uses 0.5.4 tag which is 2 months old:

s.source = { :git => "https://github.com/andreamazz/AMScrollingNavbar.git", :tag => '0.5.4' }

Thanks for you work!

Should add some code in - (void)scrollWithDelta:(CGFloat)delta

It is just several lines, so I didn't submit a request.

The isExpanded and isCollapsed should be set to NO when the following situation happening.

- (void)scrollWithDelta:(CGFloat)delta
{
    CGRect frame;

    if (delta > 0) {

        if (self.isExpanded) {
            self.isExpanded = NO;
        }
        ...
    }

    if (delta < 0) {

        if (self.isCollapsed) {
            self.isCollapsed = NO;
        }
        ...
    }
}

bar button items are tinted when there is no scroll view to follow

I haven't completely nailed this down yet but it seems to be tinting (or maybe partially obscuring with the tinted layer?) the left and right bar button items on the navigation controller's push and pop events. Seems to only happen when I'm not following any scroll view. I'll try to get more info.

Update: this doesn't happen in 0.5.2 (Haven't tried 0.5.3 yet -- was using :head)

Panning gesture

Using the pan gesture to get back to the previous controller creates a gap between the nav bar and current controller.

Please refer to the screenshots from your demo.

ios simulator screen shot 25 mar 2014 15 24 26
ios simulator screen shot 25 mar 2014 15 24 37

What's if no StatusBar?

My application do not show the status bar on top, that brings some weird issue? how can i fix this?

Black gap in a subclass of AMScrollingNavbarTableViewController

amissue

Hey, thanks for the library, it's awesome. I'm facing an issue. On the gif above you can see 2 tables. 1st one is AMScrollingNavbarViewController with UITableView in it. And this one works great. 2nd is a UITableViewController deriving from AMScrollingNavbarTableViewController. This one creates a black gap, i think of size of the navigation bar. I setup the views in storyboard and except for standard data source things I did

[self followScrollView: self.tableView];

in viewDidLoad. Any hints about this problem?
Thanks in advance.

uisearchbar in wrong position

Hi, I got some problems when trying to add search bar above table view

photo 1

photo 2

I've no idea how to fix it, can you help me?

thanks for your help

Temporarily disable scrolling

Whats the best way to temporary disable scrolling? I already tried to disable scrolling on my scrollview and setting followscrollview to nil but the navbar continues hiding.

View show under nav bar in non-autolayout mode

I don't know is it a bug, but below findings

  • (void)showNavbar

if (self.scrollableView != nil) {
if (self.isCollapsed) {
CGRect rect;
if ([self.scrollableView isKindOfClass:[UIWebView class]]) {
rect = ((UIWebView*)self.scrollableView).scrollView.frame;
} else {
rect = self.scrollableView.frame;
}

        NSLog(@"%f", rect.origin.y);

        // problem here            
            //(x, y) = (0. -64)
            //(w, h) = (320,563)
            // Non-Auto Layout

            //(x, y) = (0. 0)
            //(w, h) = (320,499)
            // Auto layout

After scroll, the scrollview inset is wrong

Hi, I am using this in a UIViewController, as a child view controller of a uitabbarcontroller. After scroll, the tableView (root view in UITableviewController) will have an offset below the navigation bar. The extra space is about the height of the navigation bar. From Xcode, if I uncheck the Adjust Scroll View Insets checkbox for the view controller, the extra space will disappear after scroll, but the content section will go under the nav bar before the scroll.

To repro this, go with the instruction video, but after embedding the UIViewController with a navigation controller, connect this navigation controller to a UITabbarController. I am using the latest version 1.0, installed via cocoapod.

sending app to background causes issues with nav bar and scroll view

When I have my nav bar in a collapsed state and then send my app to the background (for example, press the home button) then bring my app back - the nav bar is displayed. However the tinted bar is still covering the title AND my scroll view now goes behind the nav bar. navigating away and back to the same view fixes it.

installed via cocoapods - version 0.5.1 (also tested in 0.5)

Sometimes navigation items was not set alpha value correctly

If bar button item was not an array. Sometimes it wasn't set the alpha value correctly.
One that I encountered was their alpha value became 0.0f and even it was set back to 1.0f it's still invisible.

This is the original code.

- (void)updateNavbarAlpha:(CGFloat)delta
{
    ...
    [self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* obj, NSUInteger idx, BOOL *stop) {
        obj.customView.alpha = alpha;
    }];
    [self.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* obj, NSUInteger idx, BOOL *stop) {
        obj.customView.alpha = alpha;
    }];
    ...
}

This is how I managed to fixed that.

- (void)updateNavbarAlpha:(CGFloat)delta
{
    ...
    [self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* obj, NSUInteger idx, BOOL *stop) {
        obj.customView.alpha = alpha;
    }];
    self.navigationItem.leftBarButtonItem.customView.alpha = alpha;
    [self.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* obj, NSUInteger idx, BOOL *stop) {
        obj.customView.alpha = alpha;
    }];
    self.navigationItem.rightBarButtonItem.customView.alpha = alpha;
    ...
}

How to alway make scrollview indicator visible and stable.

I see the trick that hide the scrollview indicator jitter.
[[self scrollView] setShowsVerticalScrollIndicator:NO];

But compare the Instagram and Facebook. The indicator always visible.
Is there any way make things happen exactly like Instagram?

CollectionView Issue

When I scroll up my, my entire CollectionView Y Cord scrolls up with it leaving a gap in the middle? This is a ViewController with a CollectionView in it.

It also sends a View (the button you see in the middle) its supposed to stay and not move. How can I make the button stay still when it scrolls?

oops

oops1

UITableViewController "out of box" install yields black gap

Hi, thanks for this great library. ๐Ÿ‘

I couldn't find any documentation on using the project with UITableViewController, and when I use it with a table view controller "out of the box":

[self followScrollView:self.tableView]

there is a 64px gap added to the frame as soon as I start scrolling.

  1. How do I get rid of that gap?
  2. Since this is likely the most common integration scenario, it seems like documentation should exist for this.

Working with a subclassed UiTableViewController

Hello,

The sample tableview controller sample uses AMScrollingNavBar which is itself a UIViewController.

Before I try and look under the hood, just how feasible do you think it would be to get it working with my subclassed UITableViewController:

(master class which is a UITableViewController)
CoreDataTableViewController ->
(sub class of CoreDataTableViewController)
UsesTableViewController

any thoughts?

thank you.

Panning gesture

It would be good to have a feature that blocks the pan gesture on the X-axis when a user let's say pans on the Y-axis and the navbar hides. One pan gesture at a time.

What if no IBOutlet?

What if I programatically made a UITableViewController. There is no View where I can connect an IBOutlet for tableView, therefore

[self followScrollView:self.tableView]; --> will not work for me... how can I call this method in my case?

ScrollableView frame problem after scrolling

Hi,

I found a problem in line 353 of your category:
frame.size.height = [UIScreen mainScreen].bounds.size.height - frame.origin.y;
In my case, the scrollable view is the content of a tab bar view controller. With the line above the scrollable view will be resized by ignoring the tab bar.

As improvement, I would appreciate if you could change this part like this (line numbers refers to commit eef1dbd)

  1. Line 40: New property for initial height of the scrollable view
- (void)setInitialScrollableViewHeight:(float)initialScrollableViewHeight { objc_setAssociatedObject(self, @selector(initialScrollableViewHeight), [NSNumber numberWithFloat:initialScrollableViewHeight], OBJC_ASSOCIATION_RETAIN); }
- (float)initialScrollableViewHeight { return [objc_getAssociatedObject(self, @selector(initialScrollableViewHeight)) floatValue]; }
  1. Line 50: Reset the height if followScrollView: is called
[self setInitialScrollableViewHeight:0.0f];
  1. Line 202: Save the height of the scrollable view's superview on first scrolling
if ([self initialScrollableViewHeight] == 0.0f) {
        [self setInitialScrollableViewHeight:self.scrollableView.superview.bounds.size.height];
}
  1. Line 353: Use initialScrollableViewHeight instead of [UIScreen mainScreen]
    if (frameNav.origin.y == [self statusBar]) {
        frame.size.height = [self initialScrollableViewHeight];
    } else {
        frame.size.height = [self initialScrollableViewHeight] + frameNav.size.height;
    }

Thank you!

UITableView with UIRefreshControl

Nice project, congratulations!

I have problems when I use the UIRefreshControl in a UITableView.
In some cases the effect latch, but returns. In the other UITableView disappears.

It would also be interesting if there was an option to remove the followScrollView.

I dont know if there's interest in it responds to iOS 6 in cases where the app has status bar fixed.

not working with google ads

some odd issue, I tried to make it work with a web view.
when I Enabling the scrolling it's working great, but when I flip my device to horizontal my google ad disappear , is there any way to fix it ?

Push segue animation issue

I'm using AMScrollingNavbar in iOS 7 to hide the navigation bar when scrolling in a UITableView. However, when I tap on a cell's header to segue to a new view controller, the navigation bar of the destination view controller does a resize animation to 64px (44px navigation title bar + 20px status bar) and it's very noticeable. When I pop back to the first view controller, the navigation bar maintains that 64px, and not the 20px (only the status bar) it originally had before the segue.

Is there a way to prevent this or is this a bug?

Twitchy scroll indicator

First of all, this library is awesome and thanks a lot for sharing it with community!

I've found a little but confusing bug (devil is in the detail). Here is a little description:
When you scroll down and AMScrollingNavbar starts hiding (you have to do it slowly) and you gently move your finger in other direction (left or right) but still keep scrolling down you'll face a little twitching of scroll indicator.

I've also tested your demo app and it is facing the same issue. For a better visiualization (I hope becuse the movie quality is poor) take a look at this movie.

I suggest you test it by yourself! And answer me if you've also had the same problem or maybe a solution! I know you can always hide scroll indicator but it's nice to show user when he currently is :)

Insets problem with scrollViewShouldScrollToTop:

When you set insets in code to fulfil you needs (e.g. extend under UITabBar) scrollViewShouldScrollToTop: method stops working when you scroll to the bottom of UITableView.
In my code I've set [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 44, 0)]; because last row of UITableView was missing and this bug occurred. Also, when you set Adjust Scroll View Insets the same bug appears.

I don't know how to make all rows visible and keep scrollViewShouldScrollToTop: working everytime?

Extra space (like the navigatioBar height)

Hi,
It should be out off topic.
When I add the library to my VC and start scrolling the tableview an extra space appears between the status bar and my tableview.
I don't use storyboard neither xib , and I suspect it's due to the constraints.
Could you tell me how can I add them by code?
Thanks

nav bar items not fading out

Having a issue with my nag bar items still appearing a few pixels in the status bar and not getting the ease out animation when scrolling up

bug: The navigation bar dismiss sometimes

When I scroll up the scrollview, sometimes it works well. But sometimes my UIBarButtonItem didn't show correctly, in some case the navigation bar dismiss. I can not get the bar back when I scroll down.

change UIViewController to UITableViewcontroller

hi all.
i'm testing AMScrollingNavbar in UITableViewController not UIViewController in UIViewController+ScrollingNavBar.h, m.
and some changes "AMTableViewController"
remove tableView Pointer, and delegate, something.

it's work fine in UINavigationController -> UITableViewController.(rootView)

but this is not work.
NavigationController -> TabBarViewController -> UITableViewController(Tab)
there are something Black Cells in top table.

did you experience this happen?

i'll send you my samples. can u test me?

please let me know your emails thanks!

Add tap recognizer and support for tabbars

Is it possible to add a tap gesture recognizer to show the navbar if the user taps the status bar? Also, when using this with a view controller in a tabbar, there is an offset in the origin.y of the frame, I have attempted a fix, but it's not going very well, so if you have any suggestions, that'd be great! Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.