我想在UICollectionViewCell
的左上角添加按钮。我能够在单元格的左上角添加按钮,但它不会出现在单元格的顶部,而是出现在单元格下方。请告诉我该怎么做?
_deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, delButtonSize, delButtonSize)];
_deleteButton.center = CGPointMake(9, 10);
_deleteButton.backgroundColor = [UIColor clearColor];
[_deleteButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
[cell addSubview:_deleteButton];
[_deleteButton addTarget:self action:@selector(deleteRecipe:) forControlEvents:UIControlEventTouchUpInside];
答案 0 :(得分:2)
您是否尝试将其添加到单元格的内容视图中?
[cell.contentView addSubview:_deleteButton];
答案 1 :(得分:2)
在UIView中使用bringSubviewToFront:
[cell bringSubviewToFront:_deleteButton];
要在单元格界限的左上角实现效果,你应该按照Islam Q的答案,将删除按钮的框架移出单元格的边界,并将单元格的剪辑子视图设置为NO。
答案 2 :(得分:1)
确保您的cross_30
png文件1x版本大小与delButtonSize
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//cell = ...
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, delButtonSize, delButtonSize);
[testButton addTarget:self action:@selector(deleteRecipe:) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:testButton];
// ...
return cell;
}
它应该在单元格的左上方显示一个带有redColor的按钮。
此代码仅用于测试目的。理想情况下,您不希望为每个单元格创建UIButton
的新实例。而是在StoryBoard
或nib
文件中创建单元格的位置。在那里添加UIButton
。因此iOS可以使用按钮重复使用单元格。
确保单元格上的所有视图(删除按钮除外)都有左上填充。左侧和顶部的空格将使origin
(0,0)处的删除按钮看起来像在单元格的左上边界之外。
还有一些其他方法涉及子类化UIButton。但那些可能导致棘手的情况。我更喜欢在你的sutuation中重新排列视图。
答案 3 :(得分:1)
将center
的{{1}}设置为{0:0},并要求button
不要删除超出其边界的子视图:
cell
答案 4 :(得分:0)
我认为您正在为所有单元格使用1个按钮,尝试为1个单元格创建1个按钮:
require 'underscore'
found = _.any cart, (elem) -> elem.id == e.model.item.id
if !found:
@addToCart e
答案 5 :(得分:0)
这是小菜一碟:)
我抓住了这个source code,然后像这样将广告代码放入其中。我的解决方案将使用AutoLayout
。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// [...] some code from the source code
collectionImageView.layer.cornerRadius = 10;
collectionImageView.clipsToBounds = YES;
cell.backgroundColor = [UIColor clearColor];
cell.clipsToBounds = NO; // display subviews which will be out of bounds
CGFloat deleteButtonSize = 20;
UIButton *yourButtonView = [UIButton new];
yourButtonView.backgroundColor = [UIColor redColor];
yourButtonView.layer.cornerRadius = deleteButtonSize / 2.0;
// [yourButtonView addTarget:self action:@selector(deleteRecipe:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:yourButtonView];
// setup autolayout constraints
[yourButtonView setTranslatesAutoresizingMaskIntoConstraints:NO];
[NSLayoutConstraint constraintWithItem:yourButtonView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:deleteButtonSize].active = YES;
[NSLayoutConstraint constraintWithItem:yourButtonView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:deleteButtonSize].active = YES;
[NSLayoutConstraint constraintWithItem:yourButtonView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeLeft multiplier:1 constant:deleteButtonSize * 0.2].active = YES;
[NSLayoutConstraint constraintWithItem:yourButtonView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeTop multiplier:1 constant:deleteButtonSize * 0.2].active = YES;
// deleteButtonSize * 0.2 is the offset here
return cell;
}
结果如下:
答案 6 :(得分:0)
我建议您设计相应设计的自定义UICollectionViewCell
,并将其重复使用给定的标识符。