TableViewController中的UISearchBar在segue和更改旋转后向上移动

时间:2015-11-21 17:36:00

标签: ios swift

我有master-detail应用程序,我使用UISearchController来过滤UITableViewController(master)中的单元格。我有问题在segue之后消失UISearchBar。重现问题的步骤:

  1. 以纵向模式运行应用
  2. 触摸SearchBar并键入搜索查询
  3. 触摸TableView中的一个单元格并转到详细控制器
  4. 在详细控制器中,将设备的方向更改为横向模式
  5. 回想主人。现在,SearchBar部分隐藏在导航栏后面,即使在将导演定向回到肖像后也是如此。
  6. 我的主控制器的MWE:

    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
        }
    
    }
    

    截图:

    Initial state of master controller Master Controller after changing rotation and segueing back Master Controller after changing rotation and segueing back

0 个答案:

没有答案