Ios collectionViewLayout sizeForItemAtIndexPath->;奇怪的行为
因此,有趣的是,我尝试使用Ios collectionViewLayout sizeForItemAtIndexPath->;奇怪的行为,ios,objective-c,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,因此,有趣的是,我尝试使用collectionViewLayout sizeFormatindeXPath在collectionView中一次只显示一个项目并水平滚动,我尝试了以下方法: - (void)viewDidLoad { [super viewDidLoad]; flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.scrollDirection = UICollectio
collectionViewLayout sizeFormatindeXPath
在collectionView
中一次只显示一个项目并水平滚动,我尝试了以下方法:
- (void)viewDidLoad {
[super viewDidLoad];
flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumInteritemSpacing = 0.0;
flowLayout.minimumLineSpacing = 0.0;
_obj_CollectionView.pagingEnabled = YES;
_obj_CollectionView.collectionViewLayout = flowLayout;
self.obj_CollectionView.delegate = self;
self.obj_CollectionView.dataSource = self;
_obj_CollectionView.backgroundColor = [UIColor redColor];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height);
}
以下是collectionView屏幕截图:
这里紫色是我的单元格,红色是我的collectionView
但是我想要整个紫色
所以请引导我。我很困惑 我做了一点概念证明,它对我来说就像预期的那样。 您的
CollectionViewCell
可能有问题(约束错误/缺失)
首先,您不需要以编程方式创建
UICollectionViewFlowLayout
的对象。也不需要在viewDidLoad()
中编写代码。所有这些都可以使用情节提要
和委托
方法来完成
以下是您的问题代码:
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
}
}
extension ViewController : UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.purple : UIColor.blue
return cell
}
}
extension ViewController : UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
故事板截图:
输出屏幕:
您可以使用一些调试选项来检查原因:
UICollectionView的框架NSLog()
NSLog()
检查返回的实际大小CGSize
- 尝试设置硬编码大小以检查行为
- 如果使用
,请在删除约束后重试自动布局
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,弱)IBUICollectionView*clcnView;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
#pragma标记-集合视图方法-
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*重用=@“单元”;
UICollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:ReuseforIndeXPath:indexPath];
cell.backgroundColor=randomColor();
返回单元;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回10;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath{
CGFloat fl=self.clcnView.frame.size.width;
CGSize mElementSize=CGSizeMake(fl-1,fl-1);
返回熔化尺寸;
}
-(CGFloat)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout分区索引的最小元素间距:(NSInteger)分区{
返回1.0;
}
-(CGFloat)集合视图:(UICollectionView*)集合视图布局:(UICollectionViewLayout*)集合视图布局分区索引的最小线间距:(NSInteger)分区{
返回1.0;
}
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout插入分区索引:(NSInteger)分区{
返回UIEdgeInsetsMake(0,0,0,0);
}
UIColor*randomColor(){
返回[UIColor COLOR WITH RED:arc4random_uniform(255)/255.0f绿色:arc4random_uniform(255)/255.0f蓝色:arc4random_uniform(255)/255.0f alpha:1.0];
}
@结束
这就成功了。
以下是截图
在IPhone中
在iPad中
我希望这能解决您的问题。请尝试一下,它会对您有所帮助
#pragma mark -- Collection View Delegate and datasource method --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 40, 22)];
title.tag = 200;
title.backgroundColor = [UIColor purpleColor];
[cell.contentView addSubview:title];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = self.collectionViewSearchCity.frame.size.width/2-20;
return CGSizeMake(width, 50.0);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 15, 0,15);
}
试试这个:
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1.0
}
func collectionView(collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1.0
}
ViewDidload:
layout.minimumInteritemSpacing = 20
layout.minimumLineSpacing = 10
CellforrowAtIndexPath:
cell.image.clipsToBounds = true
cell.contentView.frame = cell.bounds
您是否可以尝试在sizeForItemAtIndexPath中打印集合视图框架并在ViewDidDisplay中进行比较?我尝试过,它的两个框架都相同OK您是否可以打印集合视图的headerview和footerview大小或节头大小没有节头或foote视图仍然不相同您面临的问题是什么?屏幕我贴的照片。我有同样的手机。视图在整个单元格中不是紫色的。信息技术正如你在截图中看到的,我已经更新了答案,并向UICollectionViewDelegateFlowLayout添加了一个新方法。看一看。我已经设置了适当的限制条件,并多次检查,但没有得到。你也试过了。试过了所有的方法,但仍然没有弄明白:)你是否遵循了这里提到的所有步骤。在这里工作正常。此外,如果您实现了任何布局代码,如自定义布局中的代码,请将其删除,并让布局保持为故事板中的流(默认)。什么是“CellForRowatineXpath?”?
layout.minimumInteritemSpacing = 20
layout.minimumLineSpacing = 10
cell.image.clipsToBounds = true
cell.contentView.frame = cell.bounds