8

In iOS7 the navigationBar in Safari automatically shrinks when scrolling. So does the navigationBar in Facebook, even to a point where it completely vanishes.

How would you implement this behavior yourself? I guess you would also have to dynamically adjust the contentOffset and I guess that would also collide with the default Refresh Control, wouldn't it?

Infinite
  • 2,931
  • 2
  • 27
  • 33
  • Ask on the internal Apple forums, probably a better place to get the answer. If you get it answer your own question as others are surely interested. – David H Oct 19 '13 at 12:31
  • Also, look at this answer - how to animate the bar open and closed: http://stackoverflow.com/a/2079655/1633251 – David H Oct 19 '13 at 15:35
  • @DavidH that unfortunately doesn't help me. I dont want to hide the navigationbar in one go, i want it to change while people are scrolling and depending on how much they scrolled. – Infinite Oct 19 '13 at 16:49
  • 2
    Right. So using gesture recognizers and monitoring scroll view delegate messages you open and close it at various times. That code you will have to write yourself. – David H Oct 20 '13 at 12:21
  • You can get the code to handle the scrolling behavior here : http://stackoverflow.com/questions/19819165/imitate-ios-7-facebook-hide-show-expanding-contracting-navigation-bar It's not as clean as Safari behavior but it's a start. – CedricSoubrie Jan 31 '14 at 11:35
  • 2
    Related .. http://stackoverflow.com/questions/19819165/imitate-ios-7-facebook-hide-show-expanding-contracting-navigation-bar – Fattie Feb 28 '14 at 20:43

1 Answers1

0

That's not right way of doing this, but it worked for me. I created category on UINavigationBar and overrode sizeThatFits: there like this:

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize cSize = self.frame.size;
    BOOL isPortrait = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
    CGFloat originalHeight = isPortrait ? 44: 32;
    cSize.height = self.tag > 0 ? originalHeight / 2 : originalHeight;
    return cSize;
}

Then when I need to shrink navigation bar (in scroll view's delegate methods):

CGRect navBarFrame = self.navigationController.navigationBar.frame;
BOOL isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
CGFloat originalHeight = isPortrait ? 44: 32;
navBarFrame.size.height = expand ? originalHeight : originalHeight / 2;
self.navigationController.navigationBar.tag = expand ? 0 : 1;
[UIView animateWithDuration:0.25 animations:^{
    self.navigationController.navigationBar.frame = navBarFrame;
    [self.navigationController.view setNeedsLayout];
}

Also, you may want to hide navigation items (like UIBarButtonItems) inside animation.

Almas Sapargali
  • 451
  • 4
  • 9