programing

UICollectionView의 UIRefreshControl은 컬렉션이 컨테이너 높이를 채우는 경우에만 작동합니다.

kingscode 2023. 6. 10. 15:46
반응형

UICollectionView의 UIRefreshControl은 컬렉션이 컨테이너 높이를 채우는 경우에만 작동합니다.

추가하려고 합니다.UIRefreshControl아주UICollectionView그러나 문제는 컬렉션 보기가 상위 컨테이너의 높이를 채우지 않으면 새로 고침 컨트롤이 나타나지 않는다는 것입니다.즉, 스크롤이 필요할 정도로 컬렉션 보기가 길지 않으면 새로 고침 컨트롤 보기를 표시하기 위해 아래로 끌 수 없습니다.컬렉션이 상위 컨테이너의 높이를 초과하면 바로 아래로 당겨지고 새로 고침 보기가 표시됩니다.

저는 간단한 iOS 프로젝트를 설정했습니다.UICollectionView기본 뷰 내부, 컬렉션 뷰에 대한 출구가 있어 추가할 수 있습니다.UIRefreshControl에 있어서는.viewDidLoad재사용 식별자가 있는 프로토타입 셀도 있습니다.cCell

이것은 컨트롤러의 모든 코드이며 문제를 잘 보여줍니다.이 코드에서 셀의 높이를 100으로 설정했는데, 이는 디스플레이를 채우기에 충분하지 않기 때문에 보기를 끌 수 없고 새로 고침 컨트롤이 표시되지 않습니다.디스플레이를 채우기 위해 더 높은 값으로 설정하면 작동합니다.아이디어 있어요?

@interface ViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [self.collectionView addSubview:refreshControl];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [collectionView dequeueReusableCellWithReuseIdentifier:@"cCell" forIndexPath:indexPath];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width, 100);
}

사용해 보십시오.

self.collectionView.alwaysBounceVertical = YES;

에 대한 전체 코드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;

특성/스크롤 보기/Storyboard/Xib에서 수직으로 바운스

여기에 이미지 설명 입력

래리의 신속한 대답:

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

스위프트 3:

    let refreshControl = UIRefreshControl()
    refreshControl.tintColor = .blue
    refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    collectionView.addSubview(refreshControl)
    collectionView.alwaysBounceVertical = true

만약 당신이collectionview세로로 스크롤할 수 있을 정도로 큰 콘텐츠 크기를 가지고 있습니다. 문제는 없지만 사용자의 경우에는 그렇지 않습니다.

속성을 사용하도록 설정해야 합니다.AlwaysBounceVertical설정할 수 있습니다.self.collectionView.alwaysBounceVertical = YES;

나도 같은 문제에 직면하고 있었고, 나는 그것을 사용할 수 없었습니다.UIRefreshControl까지UICollectionView의 콘텐츠 크기는 세로로 스크롤할 수 있을 정도로 컸습니다.

설정bounces의 재산.UICollectionView이것을 해결했습니다.

[self.collectionView setBounces:YES];
[self.collectionView setAlwaysBounceVertical:YES];

전화했습니다beginRefreshing()바로 뒤에viewDidLoad()하지만 일부 화면에서는 작동하지 않습니다.그리고 오직collectionView.layoutIfNeeded()viewDidLoad()나를 도왔습니다

수집 보기가 새로 고침 상태인 경우 api 호출을 체크인한 후 새로 고침을 종료하여 새로 고침 컨트롤을 해제해야 합니다.

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

언급URL : https://stackoverflow.com/questions/14678727/uirefreshcontrol-on-uicollectionview-only-works-if-the-collection-fills-the-heig

반응형