简体   繁体   English

模仿iOS 7 Safari导航栏

[英]Imitate iOS 7 Safari Nav-Bar

In iOS7 the navigationBar in Safari automatically shrinks when scrolling. 在iOS7中,Safari中的navigationBar在滚动时会自动缩小。 So does the navigationBar in Facebook, even to a point where it completely vanishes. Facebook中的navigationBar也是如此,甚至完全消失。

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? 我猜您还必须动态调整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: 我在UINavigationBar上创建了类别并覆盖sizeThatFits:像这样:

- (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. 另外,您可能想在动画中隐藏导航项(如UIBarButtonItems)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM