我有两个 collectionViewLayout、verticalFlowLayout 和 fullScreenHorizontalFlowLayout。
@implementation VerticalFlowLayout
- (instancetype)init
{
if (self = [super init]) {
self.sectionInset = UIEdgeInsetsMake(12, 0, 0, 0);
self.minimumLineSpacing = 10;
self.minimumInteritemSpacing = 0;
self.itemSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), 244);
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
@implementation FullScreenFlowLayout
- (instancetype)init
{
if (self = [super init]) {
self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.minimumLineSpacing = 10;
self.minimumInteritemSpacing = 0;
self.itemSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
当我选择一个单元格时,我想使用自定义动画更改 collectionView 布局。默认动画是持续时间为 0.33 的线性动画。 …
animation objective-c ios uicollectionview uicollectionviewlayout
我正在尝试仅用 1 行实现水平滚动集合视图。我发现实现这一点的最简单方法是准备自定义流程布局:
class CollectionViewLayoutHorizontal : UICollectionViewFlowLayout {
override init() {
super.init()
//that enables self-sizing cells
self.estimatedItemSize = CGSize(width: 1, height: 1)
self.scrollDirection = .horizontal
//that one should ensure that only one line fits
//using CGFloat.greatestFiniteMagnitude aka CGFLOATMAX breaks collection view completely
self.minimumInteritemSpacing = 1000
self.minimumLineSpacing = 10
}
}
Run Code Online (Sandbox Code Playgroud)
我的所有单元格都正确显示 - 即使它们小得多,它们也位于集合视图中间彼此相邻的位置。但问题是末尾(最后一个单元格之后)有一个额外的空白空间,其大小minimumInteritemSpacing是意想不到的。
有人解决这个问题吗?
ios autolayout uicollectionview uicollectionviewlayout swift
我正在尝试创建一个简单的自定义布局以便UICollectionView以编程方式使用(即没有 Interface Builder)。
我根据文档做所有事情:
UICollectionViewLayout并准备我的布局layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)并返回相应的布局属性collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)在我返回补充视图的地方添加实现我的问题是既不
collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)
也不
layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)
正在被调用,所以我看不到我的补充意见。
我已经仔细检查了我的实现布局和补充视图的创建,但我仍然无法让它工作。
我有一个300个单元格的集合视图,由NSFetchedResultsController驱动.每隔一段时间,所有对象都会更新,所以我收到委托消息告诉我,并让集合视图处理更新,就像我查看tableview一样.不幸的是,每次发生这种情况时它会锁定主线程几秒钟......我不知道为什么.这是我的代码:
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.cascadeViewController.collectionView performBatchUpdates:^{
NSLog(@"performingBatchUpdates");
for (NSDictionary *change in self.changes) {
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
if (type == NSFetchedResultsChangeInsert) {
[self.cascadeViewController.collectionView insertItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeDelete) {
[self.cascadeViewController.collectionView deleteItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeUpdate) {
[self.cascadeViewController.collectionView reloadItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeMove) {
[self.cascadeViewController.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
}
}];
}
NSLog(@"performingBatchUpdates end");
} completion:^(BOOL finished) {
NSLog(@"completion");
// TODO: implement
// [self configureMessageAndFooterView];
}];
NSLog(@"end of …Run Code Online (Sandbox Code Playgroud) objective-c nsfetchedresultscontroller uicollectionview uicollectionviewlayout
我有一个UICollectionViewController生成具有随机颜色的细胞用于测试目的.既然UICollectionViewController嵌入了a UIScrollView,我希望scrollView与它的大小相同contentSize.
我创建了一个子类UICollectionView,实现了intrinsicContentSize方法,并将UICollectionView我的类设置为IB中的自定义类.但是intrinsicContentSize永远不会被称为.我有一个完全相同的设置UITableView并在那里它完美无缺.
有什么想法吗?
- (CGSize)intrinsicContentSize {
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有imageView和collectionView的视图.ImageView位于屏幕左上角,大小(150,250).CollectionView是全屏.collectView的单元格里面有标签.
当单元格靠近imageView时,它的宽度应该更小.当单元格低于imageView时,它应该是全宽的.这也应该在滚动时工作,因此单元格的大小应该动态变化.所以,我需要制作一个自定义布局.我想,"layoutAttributesForElementsInRect:"是一个答案.但是如何使细胞属性在某个矩形中发生变化?还是我正朝着错误的方向前进?先感谢您.

ios uicollectionview uicollectionviewcell uicollectionviewlayout
根据过滤结果,我需要更改集合视图的流布局,并使用不同类型的单元格重新加载数据.但是你不能这样做:
myCollectionView.flowLayout = ...
Run Code Online (Sandbox Code Playgroud)
那么如何更改集合视图的流布局呢?
ios uicollectionview uicollectionviewcell uicollectionviewlayout
我想知道是否可以调整单个UICollectionViewCell的大小?我一直在看layoutAttributesForItemAtIndexPath和layoutAttributesForElementsInRect之类的东西,但它们都要求你手动设置每个项目的框架,这会变得更麻烦,因为我必须在调整单元格大小后再次计算框架.我的计划是能够选择一个单元格并使其展开以适应屏幕的宽度,并将所有其他单元格向下推,而其他单元格保持其列和行.
有没有人实现这个目标?
谢谢
我UICollectionViewDelegateFlowLayout在我的ViewController中实现了,并使用以下代码指定insets:
Run Code Online (Sandbox Code Playgroud)func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return sectionInsets }
但是当我使用断点进行调试时,从不执行此函数.因为我已经为ViewController指定了UICollectionView的委托,所以我不知道出了什么问题.
我要求在纵向模式下保持UICollectionViewCell中心对齐.
最终结果应该是:
动画
我正在使用此代码执行此操作:
override public func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
var layoutAttributes: Array = layoutAttributesForElements(in: collectionView!.bounds)!
if layoutAttributes.count == 0 {
return proposedContentOffset
}
var firstAttribute: UICollectionViewLayoutAttributes = layoutAttributes[0]
for attribute: UICollectionViewLayoutAttributes in layoutAttributes {
if attribute.representedElementCategory != .cell {
continue
}
if((velocity.x >= 0.0 && attribute.center.x > firstAttribute.center.x) ||
(velocity.x <= 0.0 && attribute.center.x < firstAttribute.center.x)) {
firstAttribute = attribute;
}
}
return CGPoint(x: firstAttribute.center.x -
collectionView!.bounds.size.width * 0.5, y: proposedContentOffset.y)
}
Run Code Online (Sandbox Code Playgroud)
这适用于所有单元格,除了最后一个单元格在滚动时没有居中对齐. …
uicollectionview ×10
ios ×9
swift ×4
autolayout ×2
objective-c ×2
animation ×1
swift3 ×1
uikit ×1
xcode ×1