UIRefreshControl on UICollectionView only works if the collection fills the height of the container

前端 未结 7 1285
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 05:35

I\'m trying to add a UIRefreshControl to a UICollectionView, but the problem is that the refresh control does not appear unless the collection view

相关标签:
7条回答
  • 2020-12-02 05:49

    I'm calling beginRefreshing() right after viewDidLoad(), but on some screens it doesn't work. And only collectionView.layoutIfNeeded() in viewDidLoad() helped me

    0 讨论(0)
  • 2020-12-02 05:49

    You must check in api call if collection view is in refreshing state then end refreshing to dismiss refreshing control.

    private let refreshControl = UIRefreshControl()
     refreshControl.tintColor = .white
     refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)
     collectionView.addSubview(refreshControl)
     @objc func refreshData() {
        // API Call
     }
    // Disable refresh control if already refreshing 
    if refreshControl.isRefreshing {
        refreshControl.endRefreshing()
    }
    
    0 讨论(0)
  • 2020-12-02 05:52

    If your collectionview has a content size big enough to scroll vertically, it's OK, but in your case it's not.

    You must enable the property AlwaysBounceVertical, so you could set self.collectionView.alwaysBounceVertical = YES;

    0 讨论(0)
  • 2020-12-02 06:02

    Try this:

    self.collectionView.alwaysBounceVertical = YES;

    Complete code for a UIRefreshControl

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColor grayColor];
    [refreshControl addTarget:self action:@selector(refershControlAction) forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];
    self.collectionView.alwaysBounceVertical = YES;
    
    0 讨论(0)
  • 2020-12-02 06:02

    Larry's answer in swift:

        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = UIColor.blueColor()
        refreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
        collectionView.addSubview(refreshControl)
        collectionView.alwaysBounceVertical = true
    

    Swift 3:

        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = .blue
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        collectionView.addSubview(refreshControl)
        collectionView.alwaysBounceVertical = true
    
    0 讨论(0)
  • 2020-12-02 06:05

    Attributes/Scroll View/Bounce Vertically in Storyboard/Xib

    0 讨论(0)
提交回复
热议问题