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

6

使用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就会出现菜单折叠。 UITableView具有以下属性:
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
} 

这个条形图是半透明的,我希望保留这个效果。我缺少什么?非常感谢您的帮助。

尝试在viewDidLoad中使用self.automaticallyAdjustsScrollViewInsets = false。 - Prashant Tukadiya
根据其他研究,tableViews已经弃用了此功能:self.contentInsetAdjustmentBehavior = .never。而且,没有任何效果。 - David Seek
要明确的是,我之前和刚刚尝试设置了self.automaticallyAdjustsScrollViewInsets = false,但没有任何区别。行为相同。 - David Seek
2个回答

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

尝试使用您的实现并与我的进行比较后,我发现 tableView.contentInsetAdjustmentBehavior 是问题的原因。将其排除在外可以帮助表格视图正常工作,但每当我尝试浏览应用程序时,它会使我的应用程序冻结... - David Seek
1
我搞定了!!将矩形的y值设为0,并使用"tableView.contentInsetAdjustmentBehavior"的.always属性。 - David Seek

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


contentInsetAdjustmentBehavior = .never 对我来说无法正常工作。 - Sunkas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接