Nice programing

UICollectionViewController에서 당겨서 새로 고침

nicepro 2020. 10. 22. 22:46
반응형

UICollectionViewController에서 당겨서 새로 고침


UICollectionViewControlleriOS 6 에서 풀다운에서 새로 고침을 구현하고 싶습니다 . 이것은 UITableViewController다음과 같이를 사용하여 쉽게 달성 할 수있었습니다 .

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(startRefresh:)
    forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;

위의 내용은 네이티브 위젯의 일부로 멋진 액체 방울 애니메이션을 구현합니다.

UICollectionViewController"더 발전된" 것처럼 UITableViewController기능의 패리티를 어느 정도 기대할 수 있지만이를 구현하는 기본 제공 방법에 대한 참조는 어디에서도 찾을 수 없습니다.

  1. 내가 간과하는 간단한 방법이 있습니까?
  2. 헤더와 문서가 모두 테이블 뷰와 함께 사용하도록 UIRefreshControl되어 UICollectionViewController있음에도 불구하고 어떻게 든 사용할 수 있습니까 ?

(1)과 (2) 모두 그렇습니다.

UIRefreshControl인스턴스를의 하위보기로 추가하기 만하면 .collectionView작동합니다.

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(startRefresh:)
    forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:refreshControl];

그게 다야! 때로는 간단한 실험이 트릭을 수행하더라도 이것이 문서 어딘가에 언급 되었기를 바랍니다.

편집 :이 솔루션은 컬렉션이 활성 스크롤바를 가질만큼 충분히 크지 않은 경우 작동하지 않습니다. 이 문장을 추가하면

self.collectionView.alwaysBounceVertical = YES;

그러면 모든 것이 완벽하게 작동합니다. 이 수정 사항 은 동일한 주제에 대한 다른 게시물 에서 가져온 것입니다 (다른 게시 된 답변의 댓글에서 참조 됨).


나는 같은 해결책을 찾고 있었지만 Swift에서. 위의 답변을 바탕으로 다음을 수행했습니다.

let refreshCtrl = UIRefreshControl()
    ...
refreshCtrl.addTarget(self, action: "startRefresh", forControlEvents: .ValueChanged)
collectionView?.addSubview(refreshCtrl)

잊지 마세요 :

refreshCtrl.endRefreshing()

스토리 보드를 사용하고 있었는데 설정 self.collectionView.alwaysBounceVertical = YES;이 작동하지 않았습니다. 을 선택 Bounces하고 Bounces Vertically나를 위해 일을한다.

여기에 이미지 설명 입력


이제 refreshControl속성이 UIScrollViewiOS 10 부터 추가 되었으므로 컬렉션보기에서 직접 새로 고침 제어를 설정할 수 있습니다.

https://developer.apple.com/reference/uikit/uiscrollview/2127691-refreshcontrol

UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged];
self.collectionView.refreshControl = refreshControl;    

mjh의 대답이 맞습니다.

collectionView.contentSize이보다 크지 않은 경우 스크롤 collectionView.frame.size할 수없는 문제가 발생 collectionView했습니다. contentSize속성을 설정할 수 없습니다 (적어도 나는 할 수 없었습니다).

스크롤 할 수없는 경우 당겨서 새로 고침 할 수 없습니다.

내 해결책은 UICollectionViewFlowLayout메서드 를 하위 클래스로 만들고 재정의하는 것입니다.

- (CGSize)collectionViewContentSize
{
    CGFloat height = [super collectionViewContentSize].height;

    // Always returns a contentSize larger then frame so it can scroll and UIRefreshControl will work
    if (height < self.collectionView.bounds.size.height) {
        height = self.collectionView.bounds.size.height + 1;
    }

    return CGSizeMake([super collectionViewContentSize].width, height);
}

참고 URL : https://stackoverflow.com/questions/13085662/pull-to-refresh-in-uicollectionviewcontroller

반응형