Wijzig UICollectionViewCell-grootte op verschillende apparaatoriëntaties

Ik gebruik een UICollectionViewmet UICollectionViewFlowLayout.

Ik stel de grootte van elke cel in via de

collectionView:layout:sizeForItemAtIndexPath:

Als ik overschakel van staand naar liggend, wil ik de grootte van elke cel aanpassen aan de grootte van de CollectionView, zonder opvulruimte tussen cellen te laten.

Vragen:

1) Hoe verander ik de grootte van een cel na een rotatiegebeurtenis?

2) En, nog beter, hoe maak je de lay-out waarbij de cellen altijd op de volledige grootte van het scherm passen?


Antwoord 1, autoriteit 100%

1) U kunt meerdere lay-outobjecten onderhouden en verwisselen, maar er is een eenvoudigere manier. Voeg gewoon het volgende toe aan uw UICollectionViewController-subklasse en pas de grootte naar wens aan:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(170.f, 170.f);
    }
    return CGSizeMake(192.f, 192.f);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

Als u -performBatchUpdates:completion:aanroept, wordt de lay-out ongeldig en worden de cellen kleiner met animatie (u kunt nul doorgeven aan beide blokparameters als u geen extra aanpassingen hoeft uit te voeren).

2) In plaats van de itemgroottes hard te coderen in -collectionView:layout:sizeForItemAtIndexPath, deelt u gewoon de hoogte of breedte van de grenzen van de collectionView door het aantal cellen dat u op het scherm wilt laten passen. Gebruik de hoogte als je collectionView horizontaal scrolt of de breedte als deze verticaal scrolt.


Antwoord 2, autoriteit 16%

Als je geen extra animaties van performBatchUpdates:completion:wilt, gebruik dan invalidateLayoutom de collectionViewLayout opnieuw te laden.

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.collectionView.collectionViewLayout invalidateLayout];
}

Antwoord 3, autoriteit 6%

Ik heb het opgelost met twee UICollectionViewFlowLayout. Een voor portret en een voor landschap. Ik wijs ze toe aan mijn collectie Bekijk dinamisch in -viewDidLoad

self.portraitLayout = [[UICollectionViewFlowLayout alloc] init];
self.landscapeLayout = [[UICollectionViewFlowLayout alloc] init];
UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientationOnLunch)) {
    [self.menuCollectionView setCollectionViewLayout:self.portraitLayout];
} else {
    [self.menuCollectionView setCollectionViewLayout:self.landscapeLayout];
}

Vervolgens wijzigde ik mijn collectieViewFlowLayoutDelgate-methoden op deze manier

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize returnSize = CGSizeZero;
    if (collectionViewLayout == self.portraitLayout) {
        returnSize = CGSizeMake(230.0, 120.0);
    } else {
        returnSize = CGSizeMake(315.0, 120.0);
    }
    return returnSize;
}

Laatst wissel ik bij rotatie van een lay-out naar een andere

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
        [self.menuCollectionView setCollectionViewLayout:self.landscapeLayout animated:YES];
    } else {
        [self.menuCollectionView setCollectionViewLayout:self.portraitLayout animated:YES];
    }
}

4, Autoriteit 4%

Er is een eenvoudige manier om de celgrootte van de collectie weer te geven:

UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = CGSizeMake(cellSize, cellSize);

Niet zeker of het animatable is.


5, Autoriteit 3%

Swift: Collection View Cel die n% van de schermhoogte is

Ik hoefde een cel was die een bepaald percentage van het standpunthoogte was en de grootte van het apparaat zou formuleren.

Met hulp van hierboven is hier de oplossing

override func viewDidLoad() {
    super.viewDidLoad()
    automaticallyAdjustsScrollViewInsets = false
    // if inside nav controller set to true
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    collectionViewLayout.invalidateLayout()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 100, height: collectionView.frame.height * 0.9)
}

Other episodes