隐藏导航NavBar无法正常工作

时间:2012-06-12 12:10:57

标签: iphone objective-c ios xcode uinavigationbar

我在UIWebView上成功实现了UIGestureRecognizer,如下所示:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                          action:@selector(handleTap)];
    tap.numberOfTapsRequired = 1;

    [myUniqueWebView addGestureRecognizer:tap];

下面的代码中有一些错误,但逻辑是这样的:我想点击一个视图来隐藏NavBar,如果它已经隐藏了,我想使用相同的逻辑来显示它。它不起作用:

-(void) handleTap {
    NSInteger didHideNavBar;
    didHideNavBar = 0;
    if ((didHideNavBar = 0)) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        didHideNavBar = 1;
        NSLog(@"tap1");
    } 
    if ((didHideNavBar = 1)) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        NSLog(@"tap2");
    }
}

Xcode向我显示“tap2”消息,没有任何反应。

4 个答案:

答案 0 :(得分:1)

您应该在didHideNavBar方法之外声明handleTap,并在第一个return;的末尾添加if,如下所示:

NSInteger didHideNavBar = 0;
-(void) handleTap {
    if ((didHideNavBar = 0)) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        didHideNavBar = 1;
        NSLog(@"tap1");
        return;
    } 
    if ((didHideNavBar = 1)) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        didHideNavBar = 0;
        NSLog(@"tap2");
    }
}

答案 1 :(得分:0)

查看您的代码。你将didHideNavBar变量设置为0,然后在条件设置为1的情况下,导航栏将隐藏然后显示。你将无法在很短的时间内看到这个肌动蛋白。

答案 2 :(得分:0)

嗨,请让你的didHideNavBar静态,     因为无论何时调用 handleTap 方法,它都会将值0赋给didHideNavBar

所以进行此更改

static NSInteger didHideNavBar = 0;

答案 3 :(得分:0)

-(void) handleTap {

    BOOL isHidden = (parentViewController.navigationController.navigationBar.alpha == 0);
    self.navigationController.navigationBar.alpha = (isHidden) ? 1 : 0;
}