简体   繁体   中英

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!

You can scroll to the top of the screen by tableView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

I think what you're looking for is either tableView?.scrollRectToVisible(searchBar.frame, animated: true) or tableView?.setContentOffset(searchBar.frame.origin, animated: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM