gpt4 book ai didi

ios - UITableview scrollsToTop 按下 UINavigationbar

转载 作者:行者123 更新时间:2023-11-29 10:49:09 26 4
gpt4 key购买 nike

我有一个 UIView,带有一个 UINavigationBar、一个 UITabBar 和一个 UITableView。当我按下状态栏时,UITableView 会滚​​动到顶部,因为我已将它设置为 TRUE

我希望能够通过按 UINavigationBar 来做同样的事情,就像在某些应用程序中发生的那样。将 UITableView 设置为 scrollsToTop = TRUE 仅在用户按下 StatusBar 时有效。

最佳答案

方法一:

在你的 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.
}

仅此而已。

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

  1. 方法 2:将启用的用户交互设置为是,并设置点按手势识别器,如方法 2 中详细所示。
  2. 方法 3:使用名为 gestureRecognizer:shouldReceiveTouchUIGestureRecognizerDelegate 方法,当触摸的 View 是按钮时使其返回 NO,否则返回。详见方法三。

第 1 点中的方法 2:- 感觉脏乱/陈旧

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

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

在您的.h 文件中实现UIGestureRecognizerDelegate 协议(protocol),并在您的.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;
}

关于ios - UITableview scrollsToTop 按下 UINavigationbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com