UICollectionViewCell添加徽章

时间:2015-03-28 10:41:57

标签: ios objective-c uicollectionview

当用户按长按单元格时,我想为我的UICollectionViewCell添加徽章。这是我的代码:

- (void)activateDeletionMode:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {      
        NSIndexPath *indexPath = [_favoriteCollection indexPathForItemAtPoint:[gesture locationInView:_favoriteCollection]];
        UICollectionViewCell *cell = [_favoriteCollection cellForItemAtIndexPath:indexPath];
       //M13BadgeView *badgeView = [[M13BadgeView alloc] initWithFrame:CGRectMake(-12,-12, 24.0, 24.0)];
        M13BadgeView *badgeView = [[M13BadgeView alloc] initWithFrame:CGRectMake(200,200, 24.0, 24.0)];
        badgeView.text = @"1";
        [cell addSubview:badgeView];
        [cell bringSubviewToFront:badgeView];

    }
}

这就是我的单元格长时间点击后的样子: enter image description here

正如你所看到的那样,我的牢房里的小红币就是徽章,它总是看起来像我改变框架它永远不会改变她的位置,例如:

//M13BadgeView *badgeView = [[M13BadgeView alloc] initWithFrame:CGRectMake(-12,-12, 24.0, 24.0)];
M13BadgeView *badgeView = [[M13BadgeView alloc] initWithFrame:CGRectMake(200,200, 24.0, 24.0)];

此外,我尝试使用[cell bringSubviewToFront:badgeView];[_favoriteCollection bringSubviewToFront:badgeView];,但它没有奏效。

**注意我正在使用this library作为徽章

更新感谢现在回答设置maskToBounds为false现在我的徽章出现了,但是我又得到了一个小问题,单元格边框显示在徽章中检查: enter image description here

2 个答案:

答案 0 :(得分:4)

要使徽章正确显示,您必须确保单元格不会将其内容剪切到其边界。因此,您需要将属性clipsToBounds设置为false。这将导致徽章在其超视图框架之外呈现。

查看已使用库的代码后,我发现在更改徽章的text属性后,会调用名为autoSetBadgeFrame的方法。此功能将导致帧的更改与先前设置的帧无关。要更改该行为,您应使用两个属性horizontalAlignmentverticalAlignment指定对齐方式。明确设置框架会导致某些子图层显示不正确。您可能应该在设置文本之前将子视图添加到单元格中。此订单将导致已解释的autoSetBadgeFrame具有有效的超级视图以对齐。

关于边框:如何指定边框?

答案 1 :(得分:1)

使用:

[[cell contentView] addSubview:badgeView];