搜索栏未在导航栏

时间:2018-02-19 18:37:12

标签: swift uinavigationcontroller uinavigationbar uisearchcontroller

我使用自定义导航控制器类删除导航栏中的后退按钮,其中包含以下内容:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    }

}

在我的一个VC中,我还在导航栏中添加了一个搜索控制器,其中包含以下内容:

private func configureSearchController() {
        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self

        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true

        self.navigationItem.titleView = searchController.searchBar
        self.definesPresentationContext = true
    }

然而,似乎搜索栏仍然被标题所在的一些空白空间所抵消。我的图像搜索栏应该在后退按钮旁边对齐,但是有后退按钮,空格有空白标题,然后是搜索栏。

enter image description here

如何让搜索栏在后退按钮旁边对齐而不是间隔,因为似乎空白标题有一些空间。另外,作为一个注释,点击该空白区域会让我回到之前的VC,虽然那里没有实际的标题。

编辑:

查看层次结构,看起来像后退按钮容器视图本身占用了大量空间enter image description here

1 个答案:

答案 0 :(得分:0)

尝试在viewDidLoad

中添加此内容
let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
backButton.setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, -1000), for: .default)
navigationItem.backBarButtonItem = backButton
相关问题