我的滚动视图中有一个导航栏。(使用StoryBoard)
我想在用户点击视图时隐藏我的导航栏
当用户再次点击时,导航栏将显示
我该怎么做?
答案 0 :(得分:3)
如果您使用的是导航栏(没有控制器),则必须为导航栏的框架以及滚动视图的框架更改设置动画。在下面的示例中,我只是将导航栏从屏幕顶部移开并相应地调整滚动视图的大小。显然,您需要导航栏和滚动视图的IBOutlet
引用:
@interface ViewController ()
@property (nonatomic) BOOL navigationBarHidden;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationBarHidden = NO;
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.scrollView addGestureRecognizer:gesture];
}
- (void)handleTap:(id)sender
{
[UIView animateWithDuration:0.5
animations:^{
CGRect navBarFrame = self.navBar.frame;
CGRect scrollViewFrame = self.scrollView.frame;
if (self.navigationBarHidden)
{
navBarFrame.origin.y += navBarFrame.size.height;
scrollViewFrame.size.height -= navBarFrame.size.height;
}
else
{
navBarFrame.origin.y -= navBarFrame.size.height;
scrollViewFrame.size.height += navBarFrame.size.height;
}
self.scrollView.frame = scrollViewFrame;
self.navBar.frame = navBarFrame;
self.navigationBarHidden = !self.navigationBarHidden;
}];
}
@end
如果您正在使用自动布局,则会略有不同(您必须为约束的更改设置动画),但基本想法是相同的。如果您仅针对iOS 6及更高版本并使用自动布局,请与我们联系。
如果你正在使用导航控制器,那就更容易了,因为你可以隐藏setNavigationBarHidden
:
[self.navigationController setNavigationBarHidden:YES animated:YES];
您可以显示:
[self.navigationController setNavigationBarHidden:NO animated:YES];
如果您想在点击时执行此操作,您可以执行类似的操作(您需要IBOutlet
到滚动视图):
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.scrollView addGestureRecognizer:gesture];
}
- (void)handleTap:(id)sender
{
BOOL hidden = self.navigationController.navigationBarHidden;
[self.navigationController setNavigationBarHidden:!hidden animated:YES];
}