如何在iOS中的UICollectionViewCell的左上角添加按钮?
我想将按钮添加到单元格的左上角。我已经尝试过,但我始终在集合视图单元格内添加按钮,但在单元格上没有添加按钮,因此请告诉我如何才能执行此操作 我试过这个如何在iOS中的UICollectionViewCell的左上角添加按钮?,ios,objective-c,iphone,ipad,uicollectionviewcell,Ios,Objective C,Iphone,Ipad,Uicollectionviewcell,我想将按钮添加到单元格的左上角。我已经尝试过,但我始终在集合视图单元格内添加按钮,但在单元格上没有添加按钮,因此请告诉我如何才能执行此操作 我试过这个 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { BlogAlbumCell *cell; static NSStrin
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BlogAlbumCell *cell;
static NSString *identifier = @"UserBlogAlbum";
cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UserAlbum *user_allbum=[arr_userAlbums objectAtIndex:indexPath.row];
cell.label_blog_name.text=user_allbum.album_name;
[cell.image_blog_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_allbum.album_image]] placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];
if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
{
NSLog(@"Inside the long press section");
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[anim setToValue:[NSNumber numberWithFloat:0.0f]];
[anim setFromValue:[NSNumber numberWithDouble:M_PI/50]];
[anim setDuration:0.1];
[anim setRepeatCount:NSUIntegerMax];
[anim setAutoreverses:YES];
cell.layer.shouldRasterize = YES;
[cell.layer addAnimation:anim forKey:@"SpringboardShake"];
CGFloat delButtonSize = 30;
UIButton *delButton = [[UIButton alloc] initWithFrame:CGRectMake(-5,-5, delButtonSize, delButtonSize)];
delButton.backgroundColor = [UIColor clearColor];
[delButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
[delButton setBackgroundColor:[UIColor blueColor]];
[cell addSubview:delButton];
delButton.tag=indexPath.row;
[delButton addTarget:self action:@selector(deleteAlbum:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"singleTap"] isEqualToString:@"yes"])
{
if(indexPath.row==([arr_userAlbums count]-1))
{
[[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"singleTap"];
}
for(UIView *subview in [cell subviews])
{
if([subview isKindOfClass:[UIButton class]])
{
[subview removeFromSuperview];
}
}
[cell.layer removeAllAnimations];
// _deleteButton.hidden = YES;
[_deleteButton removeFromSuperview];
}
return cell;
}
但它不会将按钮添加到左上角
我想要这样
编辑:
这就是我现在得到的
因为您将按钮添加为单元格的子视图,当然它将是单元格中的添加按钮而不是打开。例如,图标大小为60x60,单元格大小为80x80。只需在大小与80x80单元格相同的单元格上添加不可见的(我的意思是
backgroundColor
是清晰的)UIView
,并添加图标作为该视图的子视图,然后给这个框架CGRectMake(10,10,60,60)
。最后,在左上角添加图标作为该视图的子视图,如CGRectMake(0,0,delbuttonize,delbuttonize)
。您的UI层次结构应该是这样的
UICollectionViewCell->UIView->删除按钮和应用程序图标
您在Main.storyBoard中的设计类似于: 现在,在自定义单元格中创建按钮的出口。 并在cellForItemAtIndexPath方法中设置此按钮的属性,如:
[cell.btnDelete setTag:indexPath.row];
[cell.btnDelete addTarget:self action:@selector(btnDeleteFolderTapped:) forControlEvents:UIControlEventTouchUpInside];
并创建btnDeleteFolderTapped方法:
-(void)btnDeleteFolderTapped:(id)sender
{
// handle your stuff here
}
对于单元格之间的空格,请在.m文件中输入以下代码:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(IS_IPHONE_6P)
{
return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 70) / 2, ([UIScreen mainScreen].bounds.size.width - 70) / 2);
}
else
{
return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 60) / 2, ([UIScreen mainScreen].bounds.size.width - 60) / 2);
}
}
然后转到Main.storyboard,单击collectionView设置属性,如:
我找到了一个简单的解决方法
1.将要显示的图像放在比单元格大小小的单元格中,并将其与右下角对齐
它将提供与所需相同的用户体验!。我还附上了一张图片 您将这些代码放在哪里?在cellForItemAtIndexPath中。
cellForItemAtIndexPath
不是进行此类自定义的最佳位置,但如果您操作正确,它可以工作。请共享更多代码,至少是完整的方法cellForItemAtIndexPath
。然而,我会选择自定义单元格类。检查更新问题。@Hermannkleckerth这不起作用,因为它会阻止cel上的触摸事件,而且新视图也是主视图的子视图。但这会在左侧和右侧的单元格之间创建一些额外的空间,但我不希望在您建议的单元格扫描之间有太大的间隙比这更好的东西?看到了吗?它将在单元格之间创建额外的空间。因为单元格很大,但只有它的内容很小。它只在一行中显示两个单元格。我想在一行中显示3个单元格,最小的固定间距。建议更好的东西,请它仍然是问题设置单元格背景红色,并在以下方法中使用除以3进行检查:-->-(CGSize)collectionView:(UICollectionView)collectionView布局:(UICollectionViewLayout)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)IndeXPath问题是您的collectionView单元格较大而内容视图较小,这就是单元格之间出现此空间的原因