私はセルの間に空きスペースを残してセルを垂直に均等に分配するように見える水平スクロールUICollectionView
に異なる高さのセルを持っています。それらを一番上に整列させ、各列の底に可変空白スペースを持っています。UICollectionViewのUICollectionViewCellを垂直にどのように配置しますか?
6
A
答えて
0
UICollectionViewFlowLayoutを使用してセルをレイアウトしていると思います。私はあなたがフローレイアウトでこれを行うことができるとは思わない。 UICollectionViewLayoutのカスタムサブクラスを作成して、各セルの上端を揃える必要があります。
これは非常にきれいな解決策ではありませんが、すべてのセルを同じサイズに強制し、セル内の制約を設定してコンテンツを上部に揃えるのはどうでしょうか?これは、UICollectionViewLayoutをサブクラス化することなく、あなたが行っている外観をシミュレートすることができます。
12
UICollectionViewに提供されたUICollectionViewFlowLayoutを拡張しました。次のオーバーライドは私のために働いた。
#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
3
私は私のプロジェクトのためにXamarin/MonoTouchでこれを移植し、それがレイアウトから他の派生のために有用
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. divの中にテキストを垂直に配置しますか?
- 2. 中央にグリッドを垂直に配置しますか?
- 3. UICollectionViewCellのUICollectionView
- 4. LaTeXテーブルのイメージを使ってテキストをどのように垂直に配置しますか?
- 5. divを別のdivの内部に垂直に配置するにはどうすればよいですか?
- 6. 既存のUICollectionViewにさらにUICollectionViewCellを追加します。
- 7. テーブルセルの垂直配置の問題
- 8. インラインブロック内でのCSSの垂直配置
- 9. テキストフィールドの垂直配置の設定、AS3
- 10. テーブルの垂直方向の配置?
- 11. Java JLabelのHTMLテーブルセルにテキストを垂直に配置する
- 12. divを垂直に中央に配置するにはどうすればよいですか?
- 13. divを垂直に中央に配置するにはどうすればよいですか?
- 14. 要素に直接ではなく親にfont-sizeを設定すると、垂直方向の配置にどのように影響しますか?
- 15. UICollectionViewCellとUICollectionViewの等高さ
- 16. CSSとJavascriptの垂直配置問題
- 17. iOS 7 UITextViewの垂直配置
- 18. divをテーブルセル内で垂直に配置するにはどうすればよいですか?
- 19. 相対配置とインラインブロックを使用したDIVの垂直方向の配置?
- 20. イメージ上で真ん中に垂直に配置する方法
- 21. どのように表示するにはアンドロイドの垂直線
- 22. センターテキストをラベル内に垂直方向に配置
- 23. Navbarのメニュー項目が垂直に中央に配置されています
- 24. tabularnewlineの後にコンテンツの垂直位置を変更するにはどうすればよいですか?
- 25. div内のテキストの垂直位置を制御しますか?
- 26. HTMLのテーブルセルの中央にデータを垂直に配置する方法は?
- 27. UICollectionViewにIBのUICollectionViewCellがありません
- 28. Eclipseでボタンをどのように再配置しますか?
- 29. itextsharp:どのようにテーブルを配置しますか?
- 30. IE8で垂直スクロールバーを表示するようにします。
グレート出発点となると考えていました。チャームのように働いた! –
私はこのソリューションを試しましたが、静的なセル幅と動的なセルの高さで私のために働いていません。流れ方向は水平である。 – shujaat
shujaat、あなたはこれを理解したことがありますか? – tofutim