UISearchBar in TableViewController moves up after segue and changes rotation

I have a master-detail application where I use UISearchControllerto filter cells in UITableViewController(master). I have a problem with extinction UISearchBarafter segue. Steps to reproduce the problem:

  • Running the application in portrait mode
  • Tap SearchBar and enter
  • Touch one of the cells in the TableView and go to the detailed controller.
  • While the controller is in detail, change the orientation of the device in landscape mode.
  • Go to the wizard. SearchBar now partially hides behind the navigation bar, even after you switch to portfolio orientation.

My main MWE controller:

import UIKit

class MasterViewController: UITableViewController, UISearchResultsUpdating {

    var data = ["First", "Second", "Third", "Fourth"]
    var searchController: UISearchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        configureSearchController()
    }

    override func viewWillAppear(animated: Bool) {
        self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
        super.viewWillAppear(animated)
    }

    // MARK: - Search Controller

    private func configureSearchController() {
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.sizeToFit()

        self.definesPresentationContext = true
        self.tableView.tableHeaderView = searchController.searchBar
        self.tableView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {
    }

    // MARK: - Segues

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let content = data[indexPath.row]
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.text = content
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let content = data[indexPath.row]
        cell.textLabel!.text = content
        return cell
    }

}

Screenshots:

The initial state of the main controller Master controller after changing rotation and deferring back Master controller after changing rotation and backward adjustment

+4

Source: https://habr.com/ru/post/1616875/


All Articles