Swift searchBar separates from top of tableView after searchController.active during rotation

感情迁移 提交于 2019-12-11 06:59:34

问题


//Why does the tableView containing the searchResults array, after rotation to landscape and back to portrait, push the top of the tableView down thus separating the searchBar from the top of the tableView. This occurs repeatedly after each rotation to landscape and back. This only occurs after a search is instituted. It does not happen on rotation if the searchController is not active.

class InitialViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating {

@IBOutlet weak var tableView: UITableView!

let searchController = UISearchController(searchResultsController: nil)
let b = searchController.searchBar

b.sizeToFit() // crucial, trust me on this one
b.scopeButtonTitles = ["Drugs", "Tumor", "Target"]
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

回答1:


For some unknown reason implementing the following function solved the rotation issue.

  override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) 
  {
    self.tableView.reloadData()
  }

Paste the above func after viewDidLoad function and my problem disappeared. Don't know why. Glenn Tisman



来源:https://stackoverflow.com/questions/27129839/swift-searchbar-separates-from-top-of-tableview-after-searchcontroller-active-du

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!