1

我有 a UIViewUINavigationBaraUITabBar和 a UITableView。当我按下状态栏时,UITableView滚动到顶部,因为我将其设置为TRUE.

我希望能够通过按UINavigationBar某些应用程序中发生的类似来做同样的事情。将 设置UITableViewscrollsToTop = TRUE仅在用户按下 时才有效StatusBar

4

1 回答 1

2

方法一:

TapGestureRecogniser在你的上添加一个怎么样UINavigationBar?这仅在您的导航栏上没有任何按钮时才有效。

//Create a tap gesture with the method to call when tap gesture has been detected
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(navBarClicked):];

//isolate tap to only the navigation bar
[self.navigationController.navigationBar addGestureRecognizer:tapRecognizer];

//same method name used when setting the tapGesure's selector
-(void)navBarClicked:(UIGestureRecognizer*)recognizer{
    //add code to scroll your tableView to the top.
}

就是这样。

有些人发现在向导航栏添加点击手势时,他们的后退按钮停止工作,因此您可以执行以下两项操作之一:

  1. 方法二:将启用用户交互设置为是,并设置轻击手势识别器,如方法二详细所示。
  2. 方法三:当touch的view是按钮时,使用UIGestureRecognizerDelegate调用的方法gestureRecognizer:shouldReceiveTouch并使其返回,否则返回。详见方法3。NOYES

第 1 点的方法 2: - 感觉肮脏/骇人听闻

[[self.navigationController.navigationBar.subviews objectAtIndex:1] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:1] addGestureRecognizer:tapRecognizer];

第2点的方法3:-好多了,正确的方法

在您的文件中实现UIGestureRecognizerDelegate协议.h,并在您的.m文件中添加以下内容:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // Disallow recognition of tap gestures when a navigation Item is tapped
    if ((touch.view == backbutton)) {//your back button/left button/whatever buttons you have
        return NO;
    }
    return YES;
}
于 2014-01-18T17:08:49.330 回答