Declare a button property:
@property(nonatomic,strong)UIButton *valueBtn;
Copy the code
Create a button at page initialization:
self.valueBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, width)]; self.valueBtn.titleLabel.font = [UIFont systemFontOfSize:9]; self.valueBtn.titleLabel.textAlignment = NSTextAlignmentCenter; self.valueBtn.userInteractionEnabled = NO; Self. ValueBtn. BackgroundColor = RGBCOLOR,36,36 (234); [self.valueBtnsetTitleColor:RGBCOLOR(255, 254, 254) forState:UIControlStateNormal];
[self addSubview:self.valueBtn];
Copy the code
Custom setting corner mark:
- (void)showBageValues:(NSInteger)bageValues{
NSLog(@"-----showBageValues---%ld", bageValues);
CGFloat width = 11;
if (bageValues == 0) {
self.valueBtn.hidden = YES;
NSLog(@"-----valueBtn: %f---%f--%@", self.valueBtn.frame.size.height, self.valueBtn.frame.size.height, self.valueBtn);
}else if (bageValues < 10){
NSLog(@"---self.valueBtn.hidden = NO");
self.valueBtn.hidden = NO;
CGRect frame = self.valueBtn.frame;
frame.size = CGSizeMake(width, width);
self.valueBtn.frame = frame;
self.valueBtn.layer.masksToBounds = YES;
self.valueBtn.layer.cornerRadius = width/2;
[self.valueBtn setTitle:[NSString stringWithFormat:@"%lu",bageValues] forState:UIControlStateNormal];
}else if(bageValues < 100){ self.valueBtn.hidden = NO; CGRect frame = self.valueBtn.frame; Frame. The size = CGSizeMake (width * 1.6, width * 1.1); self.valueBtn.frame = frame; self.valueBtn.layer.masksToBounds = YES; self.valueBtn.layer.cornerRadius = 6; [self.valueBtnsetTitle:[NSString stringWithFormat:@"%lu",bageValues] forState:UIControlStateNormal];
}else if(bageValues > 99){ self.valueBtn.hidden = NO; CGRect frame = self.valueBtn.frame; Frame. The size = CGSizeMake (width * 2.2, width * 1.2); self.valueBtn.frame = frame; self.valueBtn.layer.masksToBounds = YES; self.valueBtn.layer.cornerRadius = 7; [self.valueBtnsetTitle:[NSString stringWithFormat:@"99 +"] forState:UIControlStateNormal]; }}Copy the code
Problem description: Log in to account A, if the number of message horn is 6, switch to account B to log in, if account B has no message, the horn should be 0, that is, no horn is displayed, but at this time, the horn is still displayed as 6.
Analysis: At the beginning, I thought that the relevant data was not cleared when I logged out. Because the message was an integrated ring message, I kept checking the relevant documents and codes in the Demo. Surprisingly, there was almost no similar problem. Interrupt point debugging, found that after switching the account, the code at this time is the following code
if (bageValues == 0) {
self.valueBtn.hidden = YES;
NSLog(@"-----valueBtn: %f---%f--%@", self.valueBtn.frame.size.height, self.valueBtn.frame.size.height, self.valueBtn);
}
Copy the code
Then you want to know why you didn’t hide when you set hidden to YES.
Solution: If you log out of account A and switch to account B or log in to the account without switching to account B, the initialization method will be used first
self.valueBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, width)];
Copy the code
This will create another button, and while self.valuebtn. hidden = YES; Just hide the newly created button. The valueBtn created before is not hidden. If you print their addresses, you will see that the two are not the same. Therefore, the create button is changed to lazy loading.