我有 a UIView
,UINavigationBar
aUITabBar
和 a UITableView
。当我按下状态栏时,UITableView
滚动到顶部,因为我将其设置为TRUE
.
我希望能够通过按UINavigationBar
某些应用程序中发生的类似来做同样的事情。将 设置UITableView
为scrollsToTop = TRUE
仅在用户按下 时才有效StatusBar
。
我有 a UIView
,UINavigationBar
aUITabBar
和 a UITableView
。当我按下状态栏时,UITableView
滚动到顶部,因为我将其设置为TRUE
.
我希望能够通过按UINavigationBar
某些应用程序中发生的类似来做同样的事情。将 设置UITableView
为scrollsToTop = TRUE
仅在用户按下 时才有效StatusBar
。
方法一:
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.
}
就是这样。
有些人发现在向导航栏添加点击手势时,他们的后退按钮停止工作,因此您可以执行以下两项操作之一:
UIGestureRecognizerDelegate
调用的方法gestureRecognizer:shouldReceiveTouch
并使其返回,否则返回。详见方法3。NO
YES
第 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;
}