在 UITableView 中滚动到 UISearchBar 的顶部



我有一个带有大标题的UITableView。表视图的底部是一个 UISearchBar。当searchBarShouldBeginEditing触发时,我想将UITableView滚动到屏幕顶部,搜索栏位于顶部

我尝试过这样的事情,但无济于事tableView?.scrollRectToVisible(CGRect(x: 0, y: searchBar.frame.minY, width: view.frame.width, height: 400), animated: true)

希望得到一些帮助。谢谢!

您可以通过以下方式滚动到屏幕顶部 tableView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

我认为您正在寻找的要么是tableView?.scrollRectToVisible(searchBar.frame, animated: true)要么是tableView?.setContentOffset(searchBar.frame.origin, animated: true)

最新更新