如何设置UICollectionView 初始cad偏移距离怎么设置

NavigationBar设置对UICollectionView的content显示偏移影响 - loveDoDream_zzt - 博客园
NavigationBar设置对UICollectionView的content显示偏移影响,
会自动使UICollectionView的content偏移64(即44 + 20), &
也就是navbar的高度加上状态栏的高度。
为的是不让NavigationBar不挡住cell。
设置 self.automaticallyAdjustsScrollViewInsets = NO;
就可以按照自己的想的来设置尺寸了!
给一个实例,先看问题在哪,如图:
遇到同样问题或类似问题的哥们,不管怎样就是希望尺寸按照自己的来,
self.automaticallyAdjustsScrollViewInsets = NO;
这样就可以随心所欲设置尺寸了! UICollectionView(一)——整体总结 - 简书
UICollectionView(一)——整体总结
这几天有时间看了下UICollectionView的东西,才发觉它真的非常强大,很有必要好好学习学习。以前虽然用过几次,但没有系统的整理总结过。这两天我为UICollectionView做一个比较全面的整理。包括基本使用,自定义布局,自定义插入删除动画,自定义转场动画等几部分。好了,开始。
UICollectionView相对于UITableView可以说是青出于蓝而胜于蓝,它和UITableView很相似,但它要更加强大。
UITableView的布局形式比较单一,局限于行列表,而UICollectionView的强大之处在于把视图布局分离出来成为一个独立的类,你想实现怎样的视图布局,就子类化这个类并在其中实现。
UICollectionView基础
UICollectionViewFlowLayout:视图布局对象(流视图:一行排满,自动排到下行),继承自UICollectionViewLayout。
UICollectionViewLayout有个collectionView属性,
所有的视图布局对象都继承自UICollectionViewLayout。若我们要自定义布局对象,我们一般继承UICollectionViewFlowLayout就可以了。
需要实现三个协议;UICollectionViewDataSource(数据源)、UICollectionViewDelegateFlowLayout(视图布局)、UICollectionViewDelegate。
可以看得出,除了视图布局,UICollectionView几乎和UITableView一样,但这也正是它的强大之处。
1.创建UICollectionView视图
- (void)loadCollectionView
_customLayout = [[CustomCollectionViewLayout alloc] init]; // 自定义的布局对象
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_customLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource =
_collectionView.delegate =
[self.view addSubview:_collectionView];
// 注册cell、sectionHeader、sectionFooter
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
需要注意的是这几行代码的位置,及const的位置。(我经常搞乱)
@implementation YWViewController
// 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
- (void)viewDidLoad
2.实现UICollectionViewDataSource的几个代理方法
#pragma mark ---- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return _section0Array.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
cell.backgroundColor = [UIColor purpleColor];
// 和UITableView类似,UICollectionView也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
if([kind isEqualToString:UICollectionElementKindSectionHeader])
UICollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
if(headerView == nil)
headerView = [[UICollectionReusableView alloc] init];
headerView.backgroundColor = [UIColor grayColor];
return headerV
else if([kind isEqualToString:UICollectionElementKindSectionFooter])
UICollectionReusableView *footerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
if(footerView == nil)
footerView = [[UICollectionReusableView alloc] init];
footerView.backgroundColor = [UIColor lightGrayColor];
return footerV
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
return YES;
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
return (CGSize){cellWidth,cellWidth};
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
return UIEdgeInsetsMake(5, 5, 5, 5);
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
return 5.f;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
return 5.f;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
return (CGSize){ScreenWidth,44};
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
return (CGSize){ScreenWidth,22};
#pragma mark ---- UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
return YES;
// 点击高亮
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
// 长按某item,弹出copy和paste的菜单
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
return YES;
// 使copy和paste有效
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"])
return YES;
return NO;
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
if([NSStringFromSelector(action) isEqualToString:@"copy:"])
NSLog(@"-------------执行拷贝-------------");
[_collectionView performBatchUpdates:^{
[_section0Array removeObjectAtIndex:indexPath.row];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:nil];
else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
NSLog(@"-------------执行粘贴-------------");
UICollectionView自定义布局
要自定义UICollectionView布局,就要子类化UICollectionViewLayout,然后重写它的一些方法以达到我们自定义布局的需求。下来我们来看看UICollectionViewLayout类里一些比较重要的方法:
- (void)prepareL为layout显示做准备工作,你可以在该方法里设置一些属性。
- (CGSize)collectionViewContentS返回layout的size。
*- (NSArray
)layoutAttributesForElementsInRect:(CGRect)返回在collectionView的可见范围内(bounds)所有item对应的layoutAttrure对象装成的数组。collectionView的每个item都对应一个专门的UICollectionViewLayoutAttributes类型的对象来表示该item的一些属性,比如bounds,size,transform,alpha等。
**- (UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexP传入indexPath,返回该indexPath对应的layoutAtture对象。
**- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newB **当当前layout的布局发生变动时,是否重写加载该layout。默认返回NO,若返回YES,则重新执行这俩方法:
- (void)prepareL
- (NSArray
*)layoutAttributesForElementsInRect:(CGRect)
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)返回layout“最终”的偏移量,何谓“最终”,手指离开屏幕时layout的偏移量不是最终的,因为它有惯性,当它停止时才是“最终”偏移量。
下面这两个方法一般用于自定义插入删除时的动画,后面再说。
**- (UICollectionViewLayoutAttributes )initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath )itemIndexP
**- (nullable UICollectionViewLayoutAttributes )finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath )itemIndexP
本Demo的代码虽然子类化了UICollectionViewLayout,但是主要是用于自定义插入删除动画,所以本段没什么代码展示。
UICollectionView插入删除的操作及动画
插入删除的操作
添加在哪触发:
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithTitle:@"添加"
style:UIBarButtonItemStylePlain
target:self
action:@selector(addItemBtnClick:)];
self.navigationItem.rightBarButtonItem = btnI
添加的实现:
// 添加(插入item)
- (void)addItemBtnClick:(UIBarButtonItem *)btnItem
[_collectionView performBatchUpdates:^{
// 构造一个indexPath
NSIndexPath *indePath = [NSIndexPath indexPathForItem:_section0Array.count inSection:0];
[_collectionView insertItemsAtIndexPaths:@[indePath]]; // 然后在此indexPath处插入给collectionView插入一个item
[_section0Array addObject:@"x"]; // 保持collectionView的item和数据源一致
} completion:nil];
因为是练习Demo,所以暂时把删除的触发源写在了长按某Item弹出菜单的copy按钮里。实际中你可以自定义UICollectionViewCell,添加长按手势,长按抖动出现叉号,然后删除等,随你怎么做。
// copy and paste 的实现
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
if([NSStringFromSelector(action) isEqualToString:@"copy:"])
NSLog(@"-------------执行拷贝-------------");
[_collectionView performBatchUpdates:^{
[_section0Array removeObjectAtIndex:indexPath.row];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:nil];
else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
NSLog(@"-------------执行粘贴-------------");
插入删除的动画
上面已经提到了在UICollectionViewLayout类中有两个用于自定义动画的方法,两个方法分别表示动画的起始状态和终止状态,我们可以分别在方法里设置layoutAttrure来实现某种动画效果。
苹果选择了一种安全的途径去实现一个简单的淡入淡出动画作为所有布局的默认动画。如果你想实现自定义动画,最好的办法是子类化 UICollectionViewFlowLayout 并且在适当的地方实现你的动画。
一般来说,我们对布局属性从初始状态到结束状态进行线性插值来计算 collection view 的动画参数。然而,新插入或者删除的元素并没有最初或最终状态来进行插值。要计算这样的 cells 的动画,collection view 将通过 initialLayoutAttributesForAppearingItemAtIndexPath: 以及 finalLayoutAttributesForDisappearingItemAtIndexPath: 方法来询问其布局对象,以获取最初的和最后的属性。苹果默认的实现中,对于特定的某个 indexPath,返回的是它的通常的位置,但 alpha 值为 0.0,这就产生了一个淡入或淡出动画。
简而言之,就是苹果自带了插入删除时Item的淡入淡出的动画,若你想自定义更炫的动画,就子类化UICollectionViewFlowLayout类,并重写以下两个方法:
// 初始状态
- (nullable UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
attr.center = CGPointMake(CGRectGetMidX(self.collectionView.bounds), CGRectGetMaxY(self.collectionView.bounds));
attr.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.2, 0.2), M_PI);
// 终结状态
- (nullable UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
attr.alpha = 0.0f;
insert&delete.gif
UICollectionView的转场动画
不积跬步,无以至千里。
翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视图 集合视图是用灵活的和可变的布局来显示有序的数据项集合的一种方式。最常用来显示类似网格布局的项,但在iOS中不仅仅只能用来显示行和列。在集合视图中,通过继承可...
发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注
09:45字数 61697阅读 3316评论 2喜欢 85 用到的组件 1、通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FM...
用到的组件1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SDWebImage多个缩略图缓存组件 UICKeyChainStore存放用户账号密码组件 Reachability监测网络状态 DateTools友好...
父类:NSObject UICollectionViewLayout是抽象基类,你可以使用它的子类来生成对collection view的布局信息。布局(layout)对象的工作是决定在collection view的边界内如何放置cell、supplementary vi...
UICollectionView基础 UICollectionViewFlowLayout:视图布局对象(流视图:一行排满,自动排到下行),继承自UICollectionViewLayout。 UICollectionViewLayout有个collectionView属性...
涛哥离校的时候,守在火车站门口望着他的背影止不住地哭;慧姐离校的时候,拥别在车门口硬生生憋着没哭;苏乾今天也离校了,在门口送他时发现自己哭不出来了,心里像压了一块大石头,很沉很沉……
短短几天,经历着目前为止最集中的离别。自认为自己不是一个感性的人,但毕业就像洪水决...
图文/无为跑者 端午首日主义真, 户外运动一时辰。 身轻如燕气色佳, 发浸衣透湿浑身。
由于Redhat的yum在线更新是收费的,如果没有注册的话是不能使用的,即不能在线安装软件。在这种情况下,想使用Redhat系统,还想用yum源来在线安装软件,有没有办法? 答案是有办法,请往下看! 1、删除redhat原有的yum源 # rpm -aq | grep yu...
毕业季的时候,开始跑各种招聘会,投递一份又一份的简历,每天清晨的第一件事就是查看邮箱以及最新招聘信息,找工作,实属学生期间最为痛苦的阶段,本科毕业找工作时,感慨自己学历太低,研究生毕业找工作时,又不得不感慨年龄大,每一年都觉得是就业形势最为严重的一年,在2017年来临之际,...
今天将断舍离坚持到底。 整理前 整理后 舍弃的东西 整理心得:将放置半年没有穿戴过的首饰,过期的化妆品果断丢掉,整洁清爽的化妆柜让我心情愉悦!如何设置UICollectionView 初始偏移_百度知道
如何设置UICollectionView 初始偏移
我有更好的答案
答:用错了吧,只有两个以上的输出才有相位的偏移,对单个信号来说,没有偏移的说法,因为不能跟自己偏移。
采纳率:99%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Pages: 1/3
主题 : UICollectionView cell横向间距如何调整(列距调整)
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
UICollectionView cell横向间距如何调整(列距调整)&&&
级别: 新手上路
UID: 300615
可可豆: 105 CB
威望: 80 点
在线时间: 1939(时)
发自: Web Page
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
这个我设置的,如果设置大了,原来在第一行的cell会到下一行去,中间还是有间距
级别: 新手上路
UID: 300615
可可豆: 105 CB
威望: 80 点
在线时间: 1939(时)
发自: Web Page
用这个方法调他的宽度不就可以调间距吗....
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
是的,我原来也是这么想的,我一行是仨个cell,320/3的宽度,就太大了,有一个cell跑下一行去了,间距还在那
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(320.0/3.0, 20);}这个设置过了
级别: 新手上路
UID: 300615
可可豆: 105 CB
威望: 80 点
在线时间: 1939(时)
发自: Web Page
我最大设置到100才显示三个....
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
我在cell.contentView里面放入一个按钮撑满contentView,但是cell横向的间距,让相邻的俩个cell不能紧邻在一起
级别: 侠客
UID: 285890
可可豆: 442 CB
威望: 384 点
在线时间: 663(时)
发自: Web Page
应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的。
使用-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)或直接设置layout.minimumInteritemSpacing = 0;但还要配合cell的大小、section左右缩进,因为collectionView默认section左右缩进左右是0,然后cell的宽度小的话,因为要保持cell宽度不变,这样cell之间还是有间隔。用故事板拖一个collectionView,调一调很直观
级别: 新手上路
可可豆: 112 CB
威望: 113 点
在线时间: 54(时)
发自: Web Page
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
CGFloat jx=0.0f;}这样还是没用哇
Pages: 1/3
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版如何设置UICollectionView 初始偏移_百度知道
如何设置UICollectionView 初始偏移
我有更好的答案
用错了吧,只有两个以上的输出才有相位的偏移,对单个信号来说,没有偏移的说法,因为不能跟自己偏移。
采纳率:92%
来自团队:
为您推荐:
其他类似问题
高丽参的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 cad2016偏移如何设置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信