【问题标题】:UITableview scrollsToTop pressing UINavigationbarUITableview scrollsToTop 按下 UINavigationbar
【发布时间】:2014-01-18 16:50:51
【问题描述】:
我有一个UIView 和一个UINavigationBar、一个UITabBar 和一个UITableView。
当我按下状态栏时,UITableView 滚动到顶部,因为我将其设置为TRUE。
我希望能够通过按UINavigationBar 来做同样的事情,就像在某些应用程序中发生的那样。将UITableView 设置为scrollsToTop = TRUE 仅在用户按下StatusBar 时有效。
【问题讨论】:
标签:
ios
uitableview
scroll
uinavigationbar
pressed
【解决方案1】:
方法一:
在您的UINavigationBar 上添加TapGestureRecogniser 怎么样?这仅在您的导航栏上没有任何按钮时才有效。
//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.
}
真的就是这样。
有些人发现在向导航栏添加轻击手势时,他们的后退按钮停止工作,因此您可以执行以下两种操作之一:
- 方法2:将用户交互设置为yes,并设置点击手势识别器,如方法2详细所示。
- 方法3:使用
UIGestureRecognizerDelegate方法gestureRecognizer:shouldReceiveTouch,当触摸视图是按钮时返回NO,否则返回YES。详见方法 3。
第 1 点的方法 2:- 感觉肮脏/骇人
[[self.navigationController.navigationBar.subviews objectAtIndex:1] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:1] addGestureRecognizer:tapRecognizer];
第 2 点的方法 3:- 好多了,正确的方法
在您的.h 文件中实现UIGestureRecognizerDelegate 协议,并在您的.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;
}