简体   繁体   English

如果通过 Rx 数据源绑定在屏幕外加载数据,则导航栏大标题会缩小

[英]Navigation bar Large Titles are shrunk if data is loaded off-screen through an Rx datasource binding

When using large titles in the navigation bar along with a UITableView that is bound to an Rx driver data source I've noticed that if the binding and initial data load happens while the view is offscreen, when you navigate to that view it will be scrolled such that the large title is shrunk to the “minimized” position.当在导航栏中使用大标题以及绑定到 Rx 驱动程序数据源的 UITableView 时,我注意到如果在视图离屏时发生绑定和初始数据加载,当您导航到该视图时,它将被滚动这样大标题就会缩小到“最小化”位置。

Overall setup is a UITableViewController with prefersLargeTitles = true set.总体设置是一个 UITableViewController, prefersLargeTitles = true设置了prefersLargeTitles = true Tableview is setup and subsequently bound to the Rx datasource within viewDidLoad . Tableview 已设置并随后绑定到viewDidLoad的 Rx 数据源。

Example code:示例代码:

override func viewDidLoad() {
  super.viewDidLoad()
  setupTableView()
  bindToTableView()

  // ...
}

private func setupTableView() {
  tableView.register(cellType: Cell.self)
  tableView.tableFooterView = UIView()
  tableView.separatorStyle = .none

  // ...

  // We are required to first reset the data source and delegate to allow
  // for RxCocoa to take over control.
  tableView.dataSource = nil
  tableView.delegate = nil
  tableView.rx.setDelegate(self)
    .disposed(by: bag)

  // ...
}

private func bindToModel() {
  viewModel.modelDriver
    .drive(tableView.rx.items) { tableView, row, model in
      let indexPath = IndexPath(row: row, section: 0)
      let cell: Cell = tableView.dequeueReusableCell(for: indexPath)
      cell.prepare(with: model)
      return cell
    }.disposed(by: bag)
}

And by "shrunk" I mean the titles switch to this style: “缩小”是指标题切换到这种风格:

小导航标题

Has anyone else encountered this issue?有没有其他人遇到过这个问题?


Solved: As @daniel-t mentions below the problem is not caused by Rx specifically, but by the timing of when prefersLargeTitles = true .已解决:正如@daniel-t 在下面提到的,问题不是由 Rx 引起的,而是由prefersLargeTitles = true的时间造成的。 If this property is not set before the call to tableView.reloadData() occurs then the table will load in the data and scroll appropriately for non-large titles .如果在调用tableView.reloadData()之前未设置此属性,则表将加载数据并为非大标题适当滚动。 Then after large titles is set the tableview doesn't reset it's scroll position to compensate for the new, larger navigation bar area.然后在设置大标题后,tableview 不会重置它的滚动位置以补偿新的更大的导航栏区域。

The reason this manifests somewhat weirdly, even when using something like .skipUntil(...viewWillAppear) , is because the act of binding triggers an initial Rx update which reloads the tableview.即使使用.skipUntil(...viewWillAppear)类的东西,这也.skipUntil(...viewWillAppear)有些奇怪,这是因为绑定行为触发了重新加载 tableview 的初始 Rx 更新。

Your problem is elsewhere, not in the code you presented or anything to do with Rx.您的问题在别处,而不是您提供的代码或与 Rx 相关的任何内容。 The following works as intended:以下按预期工作:

class ViewController: UITableViewController {

    private let bag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Title"
        navigationController?.navigationBar.prefersLargeTitles = true

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.dataSource = nil
        tableView.delegate = nil
        tableView.rx.setDelegate(self)
            .disposed(by: bag)

        let modelDriver = Driver.just(Array<String>(repeating: "Hello world", count: 30))
        modelDriver
            .drive(tableView.rx.items) { tableView, row, model in
                let indexPath = IndexPath(row: row, section: 0)
                let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
                cell.textLabel?.text = model
                return cell
            }
            .disposed(by: bag)
    }
}

Maybe the problem has to do with how you are constructing your cells?也许问题与您如何构建细胞有关? Or maybe you are scrolling to the bottom of the list somewhere?或者您正在滚动到列表底部的某个地方?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM