有趣的是,我尝试使用collectionViewLayout sizeForItemAtIndexPath
在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
,但我想要整个紫色。
所以请指导我。我很迷惑。 :)
答案 0 :(得分:2)
首先,您不需要以编程方式创建UICollectionViewFlowLayout
对象。也不需要用viewDidLoad()
编写的代码。您可以使用storyboard
和delegate
方法完成所有这些操作。
以下是您的问题的代码:
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)
}
}
故事板截图:
输出屏幕:
答案 1 :(得分:1)
试试这个:
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
答案 2 :(得分:0)
我做了一个概念验证,它对我来说是预期的。
你的CollectionViewCell
(错误/缺失约束)可能有问题吗?
import UIKit
class CollectionViewController: UICollectionViewController {
let layout = UICollectionViewFlowLayout()
override func viewDidLoad() {
super.viewDidLoad()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView?.collectionViewLayout = layout
collectionView?.isPagingEnabled = true
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override 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 CollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return self.view.frame.size
}
}
答案 3 :(得分:0)
您有一些调试选项可以检查原因:
NSLog()
UICollectionView
的框架
NSLog()
CGSize
检查实际返回的尺寸autoLayout
,请在删除约束后尝试。希望这会有所帮助。
答案 4 :(得分:0)
好的,我明白了。 让我先发布我的故事板的布局。
我采用了iPhone6布局,并对集合视图提供了以下约束
将集合视图的宽度修改为视图的宽度
下一步我修复了集合视图的宽高比,这样它总是一个正方形
现在,我将布局从垂直方向更改为水平滚动,并添加了以下代码。
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,weak) IBOutlet UICollectionView *clcnView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - Collection View methods-
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse = @"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
cell.backgroundColor = randomColor();
return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat fl = self.clcnView.frame.size.width;
CGSize mElementSize = CGSizeMake(fl-1, fl-1);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0,0,0,0);
}
UIColor * randomColor(){
return [UIColor colorWithRed:arc4random_uniform(255)/255.0f green:arc4random_uniform(255)/255.0f blue:arc4random_uniform(255)/255.0f alpha:1.0];
}
@end
这就是诀窍。 以下是截图
在iPhone中我希望这能解决你的问题。
答案 5 :(得分:0)
请试一试它会对你有帮助。
#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);
}