首页 > 解决方案 > Scrolling to the top of the UISearchBar in a UITableView

问题描述

I have a UITableView with a large header. At the bottom of the table view is a UISearchBar. When searchBarShouldBeginEditing fires, I want to scroll UITableView to the top of the screen with the search bar at the top

I tried something like this to no avail tableView?.scrollRectToVisible(CGRect(x: 0, y: searchBar.frame.minY, width: view.frame.width, height: 400), animated: true)

Would love some help. Thanks!

标签: iosswiftuitableviewuikit

解决方案


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

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


推荐阅读