Ho celle di altezza variabile su uno UICollectionView
a scorrimento orizzontale che sembra distribuire uniformemente le celle verticalmente lasciando uno spazio vuoto tra le celle e vorrei allineare in alto e avere lo spazio vuoto variabile nella parte inferiore di ogni colonna.Come allineare verticalmente UICollectionViewCells in un UICollectionView?
risposta
Immagino che tu stia utilizzando UICollectionViewFlowLayout per impaginare le celle. Non penso che tu possa farlo con un layout di flusso. Probabilmente devi scrivere una sottoclasse personalizzata di UICollectionViewLayout per allineare i bordi superiori di ogni cella.
Questa non è una soluzione molto carina, ma che dire di forzare tutte le celle ad avere le stesse dimensioni e impostare i vincoli nella cella per allineare il contenuto verso l'alto? Questo potrebbe simulare l'aspetto che stai cercando senza dover sottoclasse UICollectionViewLayout.
Ho esteso UICollectionViewFlowLayout fornito a UICollectionView. Il seguente override ha funzionato per me.
#import "TopAlignedCollectionViewFlowLayout.h"
const NSInteger kVerticalSpacing = 10;
@implementation TopAlignedCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];
if (indexPath.item == 0) {
CGRect frame = currentItemAttributes.frame;
frame.origin.y = sectionInset.top;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
CGFloat previousFrameRightPoint = previousFrame.origin.y + previousFrame.size.height + kVerticalSpacing;
CGRect currentFrame = currentItemAttributes.frame;
CGRect strecthedCurrentFrame = CGRectMake(currentFrame.origin.x,
0,
currentFrame.size.width,
self.collectionView.frame.size.height
);
if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) {
CGRect frame = currentItemAttributes.frame;
frame.origin.y = frame.origin.y = sectionInset.top;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
CGRect frame = currentItemAttributes.frame;
frame.origin.y = previousFrameRightPoint;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
@end
ho portato questo per Xamarin/MonoTouch per il mio progetto e ho pensato che potrebbe essere utile
public class TopAlignedCollectionViewFlowLayout : UICollectionViewFlowLayout
{
const int VerticalSpacing = 10;
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(RectangleF rect)
{
var attributesToReturn = base.LayoutAttributesForElementsInRect(rect);
foreach (UICollectionViewLayoutAttributes attributes in attributesToReturn)
{
if (attributes.RepresentedElementKind == null)
{
var indexPath = attributes.IndexPath;
attributes.Frame = this.LayoutAttributesForItem(indexPath).Frame;
}
}
return attributesToReturn;
}
public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath indexPath)
{
var currentItemAttributes = base.LayoutAttributesForItem(indexPath);
UIEdgeInsets sectionInset = ((UICollectionViewFlowLayout) this.CollectionView.CollectionViewLayout).SectionInset;
if (indexPath.Item == 0)
{
var frame1 = currentItemAttributes.Frame;
frame1.Y = sectionInset.Top;
currentItemAttributes.Frame = frame1;
return currentItemAttributes;
}
var previousIndexPath = NSIndexPath.FromItemSection(indexPath.Item - 1, indexPath.Section);
var previousFrame = this.LayoutAttributesForItem(previousIndexPath).Frame;
var previousFrameRightPoint = previousFrame.Y + previousFrame.Size.Height + VerticalSpacing;
var currentFrame = currentItemAttributes.Frame;
var strecthedCurrentFrame = new RectangleF(currentFrame.X, 0, currentFrame.Size.Width, this.CollectionView.Frame.Size.Height);
if (!previousFrame.IntersectsWith(strecthedCurrentFrame))
{
var frame = currentItemAttributes.Frame;
frame.Y = frame.Y = sectionInset.Top;
currentItemAttributes.Frame = frame;
return currentItemAttributes;
}
RectangleF frame2 = currentItemAttributes.Frame;
frame2.Y = previousFrameRightPoint;
currentItemAttributes.Frame = frame2;
return currentItemAttributes;
}
}
- 1. UICollectionViewCells non visualizzato in UICollectionView
- 2. Animazione di tutte le UICollectionViewCells in un UICollectionView
- 3. Allineare verticalmente UILabel
- 4. Come allineare gli oggetti verticalmente in ASP.NET?
- 5. Come allineare verticalmente un campo di testo in AS3
- 6. Come centrare verticalmente il contenuto di UICollectionView
- 7. CSS: destro allineare un testo verticalmente ruotato
- 8. Come allineare verticalmente due div flottanti?
- 9. Allineare verticalmente un asterisco (*) con un'etichetta in un modulo HTML
- 10. Come allineare verticalmente un TextBox all'interno di uno StackPanel?
- 11. Come allineare verticalmente qualcosa all'interno di un tag span?
- 12. Come allineare verticalmente il testo multilinea all'interno di un elenco?
- 13. Come allineare verticalmente le immagini in <td>
- 14. come allineare il testo verticalmente al centro in Android
- 15. Come allineare verticalmente più tabelle o elementi grafici in Lyx
- 16. Come allineare sia verticalmente che orizzontalmente in CSS?
- 17. verticalmente allineare Hyphend testo e delle icone
- 18. allineare verticalmente colonna nella riga ionico
- 19. IE7 allineare verticalmente mezzo - non funziona
- 20. Scarse prestazioni di UIStackView in UICollectionViewCells
- 21. Come allineare verticalmente campate con testo e immagine
- 22. Come allineare verticalmente (al centro) il contenuto di UITableView?
- 23. Come allineare verticalmente immagine + testo all'interno della cella di tabella
- 24. come allineare verticalmente il testo accanto a un'immagine fluttuante?
- 25. Come allineare verticalmente porzione di testo (galleggiare) per
- 26. Come allineare verticalmente il testo della mia barra di stato?
- 27. Come possiamo allineare verticalmente il testo nella casella di modifica?
- 28. Come allineare verticalmente un elemento all'interno di un elenco utilizzando il layout relativo?
- 29. Come allineare verticalmente le immagini all'interno di un elenco o un div?
- 30. Incorporamento di UIViewcontrollers all'interno di UICollectionviewCells
Ottimo punto di partenza per altre derivazioni dal layout. Ha funzionato come un fascino! –
Ho provato questa soluzione ma non ha funzionato per me con la larghezza della cella statica e l'altezza della cella dinamica. La direzione del flusso è orizzontale. – shujaat
shujaat, lo hai mai capito? – tofutim