在 iOS7 中,Safari 中的导航栏在滚动时会自动缩小。Facebook 中的导航栏也是如此,甚至到了完全消失的地步。
您将如何自己实施这种行为?我想您还必须动态调整 contentOffset 并且我想这也会与默认的刷新控件发生冲突,不是吗?
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.