i foundthe UICollectionViewFlowLayout influnences where the new item was created but i dont't know why
It's my first time to use UICollectionView,and something i can't firgue out happened. Here are my code,and it's expected to show a row of 10 buttons.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellidentifer forIndexPath:indexPath];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(50, 50, 40,40);
btn.backgroundColor = [UIColor greenColor];
[cell addSubview:btn];
return cell;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
static NSString *cellidentifer = @"cell";
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
_col = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowlayout];
_col.backgroundColor = [UIColor whiteColor];
_col.contentSize = CGSizeMake(500, 500);
_col.dataSource = self;
_col.delegate = self;
[_col registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellidentifer];
[self.view addSubview:_col];
}
and this is what i get
then i just add a line in my code to change the uicollectionviewflowlayout* direction like this
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
it gave me this
finally i change the button-frame ,something even stranger happened,BTW,it can't be scrolled,neither vertical nor horizontal
i am totally confused now ,thank you