iOS11如何通过滚动同步大型导航栏崩溃

时间:2017-09-21 05:21:20

标签: swift ios11

将Swift 3与Xcode 9一起使用我正在使用大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
} 

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

2 个答案:

答案 0 :(得分:1)

我不知道它是否对你有用。但它的示例代码对我有用。

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()
    }
}

答案 1 :(得分:0)

我使用了它:

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顶级插图。