searchController异常行为

时间:2018-06-23 12:32:26

标签: ios swift uisearchcontroller

class SearchMembers: UITableViewController, UISearchControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.title = NSLocalizedString("MEMBERS", comment: "")

            let search = UISearchController(searchResultsController: nil)
            self.definesPresentationContext = true
            search.searchResultsUpdater = self
            search.dimsBackgroundDuringPresentation = false
            navigationItem.searchController = search
            navigationItem.hidesSearchBarWhenScrolling = false

    }

从表视图单元格执行搜索时,可以看到搜索控制器,但应该从导航栏项执行搜索时,我只会看到没有搜索栏的空间。然后,我回到上一个视图,搜索控制器栏出现在这里。

如果我再次返回搜索视图,它会恢复正常状态。

可能的错误?

enter image description here

应为: enter image description here

编辑:

我意识到,如果我从segue设置中取消选择“ Animates”选项,则在searchController视图中不会得到空白,但是回到主视图时我仍然得到空白

1 个答案:

答案 0 :(得分:0)

检查您的segue的设置方式。您是否有一个segue从视图控制器连接到viewcontroller,或者是从tableview连接到另一个视图控制器,或者您有两个segue?因为在这种情况下,您应该使用一个segue只是在不同的地方触发它

performSegue(withIdentifier: "yourSegueIdentifier")

看看这段代码,我很快为您准备好了: 您也可以从我的github上尝试一下,并将其与您的github进行比较。两种情况下均应显示搜索栏。 https://github.com/verebes1/SearchBarHelp

import UIKit

class MainViewController: UITableViewController {

    var items = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //MARK: - Tableview methods
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }

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

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showMembers", sender: self)
    }


    //MARK: - Segue setup
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        /* Put any preparation here if needed */
    }

    //MARK: Navbar Button action
    @IBAction func searchBarButtonTapped(_ sender: Any) {
        performSegue(withIdentifier: "showMembers", sender: self)
    }

}

和第二个viewController:

import UIKit

class SearchMembersController: UITableViewController, UISearchControllerDelegate {

    var members = ["First", "Second", "Third", "Fourth"]

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.title = NSLocalizedString("MEMBERS", comment: "")

        let search = UISearchController(searchResultsController: nil)
        self.definesPresentationContext = true
        //search.searchResultsUpdater = self
        search.dimsBackgroundDuringPresentation = false
        navigationItem.searchController = search
        navigationItem.hidesSearchBarWhenScrolling = false
    }


    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return members.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = members[indexPath.row]

        return cell
    }
}

Segue

Segue setup

从注释中引用您的代码:

leftButton.addTarget(self, action: #selector(Profile.searchMembers), for: .touchUpInside) self.settingsButton.customView = leftButton 

这应该调用 performSegue(withIdentifier: "profileToMembers", sender: self) 而是Profile.searchMembers。可能会出错,您可以将执行结果包装在

之类的@objc函数中
@objc func segueToMembers(){ 
     performSegue(withIdentifier: "profileToMembers", sender: self) 
} 

然后命名为

leftButton.addTarget(self, action: #selector(segueToMembers), for: .touchUpInside) self.settingsButton.customView = leftButton