【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
思路:UIButton右上角加UILabel,UILabel用来显示数字。在把UIButton加到UIBarButtonItem上去。
UIBarButton的属性:
- (instancetype)initWithCustomView:(UIView *)customView;
添加代码:
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
开始实现:
第一步:把UIButton加到UIBarButtonItem上去:
#pragma mark--
#pragma mark 增加右按钮
-(void)addRightBarItem
{
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0f, 0.0f, 60, 43);
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitle:@"我的卡券" forState:UIControlStateNormal];
rightButton.titleLabel.font = Demon_13_Font;
[rightButton addTarget:self action:@selector(mineCardCoupon) forControlEvents:UIControlEventTouchUpInside];
[rightButton addSubview:self.numberLabel];//第二步
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
第二步:UILabel显示数字
#pragma mark 我的卡券显示的数字
-(UILabel *)numberLabel
{
if (!_numberLabel) {
_numberLabel = [UILabel new];
_numberLabel.frame = CGRectMake(55, 10, 16, 16);
// _numberLabel.text = @"···";
_numberLabel.text = @"8";
_numberLabel.font = Demon_16_Font;
_numberLabel.backgroundColor = [UIColor whiteColor];
_numberLabel.textColor = [UIColor redColor];
_numberLabel.textAlignment = NSTextAlignmentCenter;
_numberLabel.layer.cornerRadius = 8;
_numberLabel.layer.masksToBounds = YES;
}
return _numberLabel;
}
效果图: