1

我想在点击后隐藏导航栏

navigationController?.hidesBarsOnTap = true

点击后导航栏正确隐藏 隐藏导航栏

但是在添加了一个 searchController 之后(下面的代码)

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController

我的视图(青色)无法正确扩展

不正确

我也试过旋转它。出现搜索栏。

在此处输入图像描述

4

1 回答 1

0

终于找到了解决办法

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.barHideOnTapGestureRecognizer.addTarget(self, action: #selector(barHideAction(_:)))
    let searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    navigationController?.hidesBarsOnTap = true
}

@objc func barHideAction(_ guesture: UITapGestureRecognizer) {
    updateFrame()
}

func updateFrame() {
    if let nc = navigationController {
        let isHidden = nc.isNavigationBarHidden
        searchController.searchBar.superview?.isHidden = isHidden

        if isHidden {
            self.additionalSafeAreaInsets.top = -64 // fixed by a magic num
        }
        else {
            self.additionalSafeAreaInsets.top = 0
        }
    }
}

示例代码

于 2017-12-16T08:17:13.087 回答