gpt4 book ai didi

ios - 点击 View 时如何隐藏/显示我的导航栏(不是导航 Controller )?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:39 25 4
gpt4 key购买 nike

我的 ScrollView 中有一个导航栏。(使用 StoryBoard)
我想在用户点击 View 时隐藏我的导航栏。
当用户再次点击时,导航栏将显示。
我怎样才能做到?

最佳答案

如果您正在使用导航栏(没有 Controller ),您必须为导航栏和 ScrollView 的框架的变化设置动画。在下面的示例中,我只是将导航栏移出屏幕顶部并相应地调整 ScrollView 的大小。您显然需要导航栏和 ScrollView 的 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 及更高版本并且正在使用自动布局,请告诉我。


如果您使用的是导航 Controller ,它会更容易一些,因为您可以使用 setNavigationBarHidden 进行隐藏。 :

[self.navigationController setNavigationBarHidden:YES animated:YES];

您可以展示:

[self.navigationController setNavigationBarHidden:NO animated:YES];

如果你想在点击时执行此操作,你可以执行类似的操作(为此你需要一个 IBOutlet 到你的 ScrollView ):

- (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];
}

关于ios - 点击 View 时如何隐藏/显示我的导航栏(不是导航 Controller )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com