代码之家  ›  专栏  ›  技术社区  ›  David Seek

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

  •  6
  • David Seek  · 技术社区  · 7 年前

    UINavigationBar 查看人:

    if #available(iOS 11.0, *) {
    
        navigationBar?.prefersLargeTitles = true
    
        UINavigationBar.appearance().largeTitleTextAttributes = [
            NSForegroundColorAttributeName: Colors.black
        ]
    
    }
    

    当我滚动 UITableView ,条折叠过快,产生不需要的空间:

    enter image description here

    之后:

    enter image description here

    当我触摸 酒吧坍塌了。

    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

    此外 设置为:

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   Vini App    7 年前

    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()
        }
    }
    
        2
  •  0
  •   David Seek    7 年前

    我使用以下方法使其工作:

    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
    )
    

    没有更多的桌面插页。