UIImageView点击隐藏导航栏

时间:2011-11-01 09:22:23

标签: iphone objective-c uinavigationcontroller uiimageview

我有一个显示来自网络图像的视图。该视图只有UIImageView。我想知道当用户点击并在用户再次重新点击视图时再次显示navigationBar时隐藏{{1}}。 (就像原生的iPhone照片应用程序一样)

我知道我可以使用这个

  

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

但我不知道在哪里使用它,在哪里放入此代码。

帮助将不胜感激

3 个答案:

答案 0 :(得分:3)

初始化新的UITapGestureRecognizer

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavigationBar:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[self.imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

您还必须确保UIImageView将userInteractionEnabled设置为 YES ,因为默认情况下它在NO上设置为UIImageView

self.imageView.userInteractionEnabled = YES;

最后,编写手势识别器识别时调用的方法。这是在手势识别器的初始化方法中的action:参数中传递的方法选择器:

- (void)toggleNavigationBar:(UITapGestureRecognizer *)tapGestureRecognizer
{
    [self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:YES];
}

答案 1 :(得分:2)

在UIImageView上放置一个UITapGestureRecognizer,在委托中调用你提到的方法。像这样:

UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[img addGestureRecognizer:g];
[g release];

然后你的代表:

-(void) imageTapped:(UITapGestureRecognizer*)tg
{
    if(self.navigationController.toolbarHidden)
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    else
        [self.navigationController setNavigationBarHidden:NO animated:YES];
}

答案 2 :(得分:0)

如果你无法弄清楚其他答案你可以作弊。您可以使用一个按钮将其设置为透明,并使用代码将IBAction链接到它:

UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake( x,y,0,0)];
imageButton.backgroundColor = [UIColor clearColor];

[imageButton addTarget:self action:@selector(navBarHide:) 
 forControlEvents:UIControlEventTouchUpInside];

-(IBAction)navBarHide {
if (!navBarHidden) {

[self.navigationController.navigationBar removeFromSuperView];

}
else {

[YourUIView addSubview: yourNavigationBar];

}
}