2

I'll find a solution here in Objective-C, but it is not working for me. I would like to hide my 'NavigationBar' in my 'TableViewController' when I scroll down. I have done it this way:

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.title = "Test"
    self.navigationBar = UINavigationBar(frame: CGRectZero)
    self.view.addSubview(navigationBar)
    self.navigationBar.pushNavigationItem(self.navigationItem, animated: false)

func layoutNavigationBar() {

    self.navigationBar.frame = CGRectMake(0, self.tableView.contentOffset.y, self.tableView.frame.size.width, self.topLayoutGuide.length + 44)
    self.tableView.contentInset = UIEdgeInsetsMake(self.navigationBar.frame.size.height, 0, 0, 0)
}

override func scrollViewDidScroll(scrollView: UIScrollView) {
    self.layoutNavigationBar()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.layoutNavigationBar()
}

Any ideas what is wrong here? The 'NavigationBar' appears on load, but never disappears.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
derdida
  • 14,784
  • 16
  • 90
  • 139
  • See link for answer -> http://stackoverflow.com/a/21049991/1922144 – davidcondrey Sep 06 '14 at 11:30
  • possible duplicate of [Imitate iOS 7 Facebook hide/show expanding/contracting Navigation Bar](http://stackoverflow.com/questions/19819165/imitate-ios-7-facebook-hide-show-expanding-contracting-navigation-bar) – davidcondrey Sep 06 '14 at 11:30
  • Ok thanks - ill found another Thread here (with the Objective C solution i provided in my Thread) - i will give this a try – derdida Sep 06 '14 at 11:59

1 Answers1

0

See RRViewControllerExtension on github that i have just posted.

With RRViewControllerExtension, you even don't have to #import the header files, all you have to do is like below:

//typically in your UIScrollViewDelegate method
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        BOOL mode;
        if(scrollView.contentOffset.y > 300)
            mode = NO;
        else
            mode = YES;

        if(mode != _previewMode)
        {
            _previewMode = mode;

            //force navigation appearance update
            [self updateNavigationAppearance:YES];
        }
    }

    -(BOOL)prefersNavigationBarTransparent
    {
        if(_previewMode)
            return NO;
        else
            return YES;
    }

    -(nullable UIColor *)preferredNavigationItemColor
    {
        if(_previewMode)
            return [UIColor whiteColor];
        else
            return [UIColor blackColor];;
    }
Roen
  • 116
  • 6