简体   繁体   中英

Prefer Large Titles and RefreshControl not working well

I am using this tutorial to implement a pull-to-refresh behavior with the RefreshControl . I am using a Navigation Bar . When using normal titles everything works good. But, when using "Prefer big titles" it doesn't work correctly as you can see in the following videos. Anyone knows why? The only change between videos is the storyboard check on "Prefer Large Titles".

用“喜欢大标题” 带有正常标题

I'm having the same problem, and none of the other answers worked for me.

I realised that changing the table view top constraint from the safe area to the superview fixed that strange spinning bug.

Also, make sure the constant value for this constraint is 0 🤯.

如果使用故事板

At the end what worked for me was:

  • In order to fix the RefreshControl progress bar disappearing bug with large titles:

     self.extendedLayoutIncludesOpaqueBars = true
  • In order to fix the list offset after refreshcontrol.endRefreshing() :

     let top = self.tableView.adjustedContentInset.top let y = self.refreshControl!.frame.maxY + top self.tableView.setContentOffset(CGPoint(x: 0, y: -y), animated:true)

如果您使用的是tableView.tableHeaderView = refreshControltableView.addSubView(refreshControl)您应该尝试使用tableView.refreshControl = refreshControl

It seems there are a lot of different causes that could make this happen, for me I had a TableView embedded within a ViewController. I set the top layout guide of the tableview to the superview with 0 . After all of that still nothing until I wrapped my RefreshControl end editing in a delayed block:

DispatchQueue.main.async {
   if self.refreshControl.isRefreshing {
       DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
            self.refreshControl.endRefreshing()
       })
   }
}

对我来说唯一可行的解​​决方案是将布鲁诺的建议与这行代码结合起来:

tableView.contentInsetAdjustmentBehavior = .always

I've faced the same problem. Call refreshControl endRefreshing before calling further API.

refreshControl.addTarget(controller, action: #selector(refreshData(_:)), for: .valueChanged)

@objc func refreshData(_ refreshControl: UIRefreshControl) {
        refreshControl.endRefreshing()
        self.model.loadAPICall {
            self.tableView.reloadData()
        }
    }

The only solution that worked for me using XIBs was Bruno's one: https://stackoverflow.com/a/54629641/2178888

However I did not want to use a XIB. I struggled a lot trying to make this work by code using AutoLayout.

I finally found a solution that works:

    override func loadView() {
        super.loadView()
        let tableView = UITableView()
        //configure tableView
        self.view = tableView
    }

I had this issue too, and i fixed it by embedded my scrollView (or tableView \ collectionView) inside stackView, and it's important that this stackView's top constraint will not be attached to the safeArea view (all the other constraints can). the top constraint should be connect to it's superview or to other view.

我长期以来一直面临同样的问题,对我来说唯一可行的解​​决方案是向 tableview 的背景视图添加刷新控制。

tableView.backgroundView = refreshControl

Short Answer

I fixed this by delaying calling to API until my collection view ends decelerating

Long Answer

I notice that the issue happens when refresh control ends refreshing while the collection view is still moving up to its original position. Therefore, I delay making API call until my collection view stops moving aka ends decelerating. Here's a step by step:

  1. Follow Bruno's suggestion
  2. If you set your navigation bar's translucent value to false ( navigationBar.isTranslucent = false ), then you will have to set extendedLayoutIncludesOpaqueBars = true on your view controller. Otherwise, skip this.
  3. Delay api call. Since I'm using RxSwift , here's how I do it.
collectionView.rx.didEndDecelerating
    .map { [unowned self] _ in self.refreshControl.isRefreshing }
    .filter { $0 == true }
    .subscribe(onNext: { _ in
        // make api call
    })
    .disposed(by: disposeBag)
  1. After API completes, call to
refreshControl.endRefreshing()

Caveat

Do note that since we delay API call, it means that this whole pull-to-refresh process is not as quick as it could have been done without the delay.

In addition to Bruno's answer, for those who struggling with not working refreshControl.endRefreshing() there is not hacky way with OperationQueue.

...
//operation queue
let ops = OperationQueue()

//operation for API fetch data and tableview reload
let reloadOp = BlockOperation {
    [weak self] in
    guard let self = self else {return}
    
    self.getData(
        payload: payload,
        filterMode: self.filterModel.mode
    ) {
        DispatchQueue.main.async {
            self.monitoringTableView.reloadData()
        }
    }
}

//operation for refreshControl end refreshing
let endRefreshOp = BlockOperation {
    [weak self] in
    DispatchQueue.main.async {
        self?.monitoringTableView.refreshControl?.endRefreshing()
    }
}

//add dependency, so that endRefreshOp will wait for reloadOp ends first
endRefreshOp.addDependency(reloadOp)
ops.addOperation(reloadOp)
ops.addOperation(endRefreshOp)

Unfortunately, no advice helped. But I found a solution that helped me. Setting the transparency of the navigation bar helped. enter image description here

Problem can be solved if add tableview or scroll view as root view in UIViewController hierarchy (like in UITableViewController)

override func loadView() {
    view = customView
}

where customView is UITableView or UICollectionView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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