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

标签 swift ios11

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

if #available(iOS 11.0, *) {

    navigationBar?.prefersLargeTitles = true

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

}

当我滚动 UITableView 时,该栏折叠得太快,造成不需要的空间:

之前:

enter image description here

之后:

enter image description here

当我触摸 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()
    }
}

关于swift - iOS11 如何同步大导航栏折叠与滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46336003/

相关文章:

swift - 属性字符串是否有 joinWithSeparator

c++ - swift 3 中是否有等效的 c++ shared_ptr?

ios - 如何快速使用 CoreData 信息对 UITableView 进行排序?

pdf - 如何在iOS11的PDFKit中添加图片标注

ios - UITableView:当 gestureRecognizers 激活时,scrollViewDidScroll 执行并且 contentOffset 跳转

ios - Xcode 9 borderColor 在用户定义的运行时属性中不起作用

ios - 将文本水平放入 UIScrollView 和适当的 AutoLayout - Swift

ios - 运行 2 个 iOS 模拟器——第二个不显示任何应用程序

ios - UIActivityViewController 共享消息取消不显示

ios - iOS11 Safari:添加到主屏幕本地存储生命周期