我正在使用Facade框架以编程方式创建Collection View Cells以使其正确定位。不幸的是,细胞内的按钮不会对触摸产生反应。 Collection View是使用storyboard创建的。 CollectionViewCell外观加载:
_likesButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_likesButton setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.10]];
_likesButton.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
_likesButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
_likesButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[_likesButton setAttributedTitle:[self attributedSpacedStringFromString:@"+10039" withColor:[UIColor flatWhiteColor] withFont:[UIFont fontWithName:@"OpenSans-Bold" size:12]] forState:UIControlStateNormal];
_likesButton.layer.cornerRadius = 12;
[_likesButton sizeToFit];
_likesButton.userInteractionEnabled = YES;
在集合视图中:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.likesButton.tag = indexPath.row;
[[cell likesButton] addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
我做错了什么? 最好的问候,
阿德里安。
答案 0 :(得分:0)
好的,我刚想通了...... 我将图像作为背景(UIButton是子视图),UIImageView默认禁用 userInteractionEnabled 。将此属性设置为YES,修复了问题。