iOS11 如何同步大导航栏折叠与滚动

iOS11 how to sync large navigation bar collapse with scroll

使用 Swift 3 和 Xcode 9 我正在使用大 UINavigationBar 查看方式:

if #available(iOS 11.0, *) {

    navigationBar?.prefersLargeTitles = true

    UINavigationBar.appearance().largeTitleTextAttributes = [
        NSForegroundColorAttributeName: Colors.black
    ]

}

当我滚动 UITableView 时,栏折叠得太快,导致出现不需要的 space:

之前:

之后:

我一碰到 UITableView 栏就塌了。

tableView 具有以下属性:

let rect = CGRect(

    x: 0,
    y: UIApplication.shared.statusBarView?.frame.height ?? 20,
    width: UIScreen.main.bounds.width,
    height: UIScreen.main.bounds.height

)

let tableView = UITableView(frame: rect)

tableView 的顶部插图是 self.navigationController?.navigationBar.frame.height ?? 44

此外 tableView 设置为:

if #available(iOS 11.0, *) {
    self.contentInsetAdjustmentBehavior = .never
} 

酒吧是半透明的,我希望保留它。我错过了什么?非常感谢帮助。

不知道对你有没有用。但它是为我工作的示例代码。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
    var numbers: [String] = []

    let rect = CGRect(
        x: 0,
        y: UIApplication.shared.statusBarFrame.height ?? 20,
        width: UIScreen.main.bounds.width,
        height: UIScreen.main.bounds.height
    )

    lazy var tableView: UITableView = {
        let tV = UITableView()
        tV.delegate = self
        tV.dataSource = self
        tV.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        return tV
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        numbers = generateNumbers()
        self.view.backgroundColor = UIColor.white
        title = "Numbers"

        self.navigationController?.navigationBar.prefersLargeTitles = true

        let search = UISearchController(searchResultsController: nil)
        search.hidesNavigationBarDuringPresentation = false
        search.searchResultsUpdater = self
        search.definesPresentationContext = true
        self.navigationItem.searchController = search
        tableView.frame = rect

        self.view.addSubview(tableView)
    }

    func generateNumbers() -> [String] {
        var numbers = [String]()
        for var i in 1..<100 {
            numbers.append(String(i))
        }
        return numbers
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func updateSearchResults(for searchController: UISearchController) {
        if let text = searchController.searchBar.text, !text.isEmpty {
            numbers = self.numbers.filter({ (number) -> Bool in
                return number.contains(text)
            })
        } else {
            numbers = generateNumbers()
        }
        self.table.reloadData()
    }
}

我使用了它:

if #available(iOS 11.0, *) {
    self.contentInsetAdjustmentBehavior = .always
} 

let tableViewFrame = CGRect(
    x: 0,
    y: UIApplication.shared.statusBarFrame.height ?? 20,
    width: UIScreen.main.bounds.width,
    height: UIScreen.main.bounds.height
)

并且没有更多的 TableView 顶部插图。