我的搜索栏位于屏幕顶部,隐藏在导航栏下方。我想在用户平移时显示搜索栏,仅当 TableView 位于顶部(第 0 行)时。谁能解释一下?
我试过使用平移手势,但每次向下平移时都会显示搜索栏。
func showSearchBar(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .Changed {
let translation = recognizer.translationInView(view)
if searchBarConstraint.constant < searchBar.bounds.height {
searchBarConstraint.constant += translation.y
}
}
}
最佳答案
由于 UITableView
是 UIScrollView
的子类,您可以使用它的 contentOffset
来确定表格 View 是否位于顶部:
let isAtTop = tableView.contentOffset.y == 0
当然,这只适用于您没有将默认 contentOffset
设置为另一点的情况。否则,您必须检查默认 contentOffset
的 y
值。
关于swift - 从第一行拖动 UITableView 时显示 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289246/