微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
}

酒吧是半透明的,我希望保持这一点.我错过了什么?非常感谢帮助.

解决方法

我不知道它对你有用.但它的示例代码我有用.

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

    let rect = CGRect(
        x: 0,y: UIApplication.shared.statusBarFrame.height ?? 20,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()
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐