- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 UICollectionViewFlowLayout 并想像下面那样应用部分插图。我所有的元素都具有相同的宽度但高度不同。当同一部分中的项目高度相同时,插图有效,但当它们在同一部分中的高度不同时,则无效。这是此布局的预期行为吗?我是否需要子类化并定制一个或缺少某些东西?
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
UIEdgeInsets insets = UIEdgeInsetsZero;
CGFloat big = 30;
CGFloat small = 10;
if (section < 5) {
insets = UIEdgeInsetsMake(0, big, 0, small);
} else {
insets = UIEdgeInsetsMake(0, small, 0, big);
}
return insets;
}
最佳答案
出于某种原因,如果您每行有一个项目,UICollectionViewFlowLayout 具有将单元格居中的行为。它会忽略插入的部分。
您可以通过覆盖 UICollectionViewFlowLayout 并更改以下两个方法来解决此问题:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
[self fixLayoutAttributeInsets:attribute];
return attribute;
}
和
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *results = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *attribute in results)
{
[self fixLayoutAttributeInsets:attribute];
}
return results;
}
魔法:
- (void)fixLayoutAttributeInsets:(UICollectionViewLayoutAttributes *)attribute
{
if ([attribute representedElementKind])
{ //nil means it is a cell, we do not want to change the headers/footers, etc
return;
}
//Get the correct section insets
UIEdgeInsets sectionInsets;
if ([[[self collectionView] delegate] respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)])
{
sectionInsets = [(id<UICollectionViewDelegateFlowLayout>)[[self collectionView] delegate] collectionView:[self collectionView] layout:self insetForSectionAtIndex:[[attribute indexPath] section]];
}
else
{
sectionInsets = [self sectionInset];
}
//Adjust the x position of the view, the size should be correct or else do more stuff here
CGRect frame = [attribute frame];
frame.origin.x = sectionInsets.left;
[attribute setFrame:frame];
}
如果您在一行中有多个单元格,则此解决方案不起作用(也不需要),因此您应该使用 bool 值或任何您喜欢的方式检查这种情况...这只是此场景的一个简单示例
关于ios - UICollectionViewFlowLayout 忽略不同高度项目的部分插图 - 错误还是预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165816/
我正在尝试创建一个具有如下用户配置文件的应用程序(第一张图片):https://dribbble.com/shots/1642506-Messaging-app-Viber/attachments/2
我有一个带有流布局的垂直滚动 Collection View 。我已经对布局对象进行了子类化,以便将元素在每一行的左侧对齐(使用从这里和那里借来的代码),并将节标题放在每个节的左边距(插图)上(而不是
给定一个具有流式布局的 UICollectionView,计算 Collection View 的列数(垂直方向)或行数(水平方向)的最简单方法是什么? 最佳答案 我的做法(假设所有元素具有相同的大小
我正在尝试完成以下任务,我已经使用 UIScrollview 轻松完成了此任务,但我最近一直在尝试 UICollectionView (我知道我玩游戏已经很晚了)并且很想知道是否可以这样做我想要什么,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SS
我正在使用 UICollectionViewFlowLayout 来显示 GridView ,这就是我所看到的。 问题出在左侧的缝隙上。我对使用 UICollectionViewFlowLayout
我想在 Collection View 中为每个单元格设置一个自定义大小的布局。正因为如此,我希望单元格能够很好地组合在一起,正如您在此处看到的那样: (第一个单元格是顶部单元格,然后左下角是数字 2
我对流布局进行了子类化,并将其设置为我的 Collection View 中的 collectionViewLayout。 然后我设置布局如下: - (id) init { if(self =
我有一个 UICollectionView,其中的项目可以在运行时修改它的高度。问题是当一个项目高于另一个项目时,项目周围有很多空白。 我正在寻找创建此属性的属性: 我想使用 UICollection
我正在使用 UICollectionViewFlowLayout 在水平 ScrollView 中排列 View 图 block 。默认情况下,我使用 UICollectionViewDelegate
我有一个 Collection View ,它被设置为一个水平的单元格行。它抛出以下约束错误(我正在使用 AutoLayout): The behavior of the UICollectionVi
我有一个 UICollectionView,它使用 UICollectionViewFlowLayout 的自定义子类,使节标题在滚动时粘在屏幕顶部,就像 UITableView 的普通样式一样。我的
我正在尝试修改 UICollectionViewFlowLayout(垂直滚动),以便将每个部分标题放置在该部分所有项目的左侧(而不是顶部,这是默认设置)。 也就是说,这是默认行为: ...这就是我想
我有一个带有 UICollectionViewFlowLayout 的 UICollectionView。我还实现了 UICollectionViewDelegateFlowLayout 协议(pro
我正在使用自定义 UICollectionViewFlowLayout使单元格在到达顶部时缩放和淡入淡出。为此,我正在申请 alpha和 transform到布局属性。这是我的代码( link to
我创建了一个继承自 UICollectionViewFlowLayout 的 Swift 类,并且我已经能够成功地完成一些巧妙的操作,例如更改它绘制的每个框之间的间距。我正在绘制日历,因此想象一下 2
我有一个相当标准的网格布局,使用 UICollectionViewFlowLayout 的子类.当用户点击一个单元格时,我会向下移动后续行中的单元格,以便为将出现的详细 View 腾出空间。它看起来像
经过一番摆弄后,我开始怀疑 UICollectionViewFlowLayout 的间距代码中存在错误。 问题很简单:我想在屏幕上显示五个按钮,我希望按钮之间的间距为 1 pt(视网膜显示屏上为 2
我有一个带有子类 UICollectionViewFlow 布局的 Collection View 。 我想要的效果是旋转(透视,圆形 y 轴)和运动缩放。 我的 Collection View 设置
我最近开始使用 UICollectionView,对 UICollectionViewFlowLayout 有点困惑。似乎 Collection View 中每个单元格的框架是在每个项目之间的相等空间
我是一名优秀的程序员,十分优秀!