发现 button设置背景图片 iOS7 与 iOS8 差异

本文探讨了在iOS7和iOS8中,UIButton设置背景图片时出现的不同行为。在iOS7中,按钮仅显示.titleLabel.textColor,而在iOS8中则使用setTitleColor来显示文字颜色。目前,造成这一差异的原因尚未明确,将继续进行观察和研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有待详细研究

iOS7: 只能显示 .titleLabel.textColor

<span style="color:#ff0000;">self.btnNum.titleLabel.textColor=[UIColor blueColor];</span>

iOS8: 只能显示 setTitleColor

<span style="color:#3366ff;">[self.btnNum setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];</span>

问题还未找到原由!有待观察。。。


代码如下:
//复用机制
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString * cellName=@"cell";
    
    HYHistoryCell * historyCell=[_tableViewHistory dequeueReusableCellWithIdentifier:cellName];
    if (historyCell==nil){
        
        historyCell=[[HYHistoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        historyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
        NSDictionary * dic = [_arrRecord objectAtIndex:indexPath.row];
        
        /*
         * 期数
         */
        historyCell.labelIssue.text=[NSString stringWithFormat:@"第%@期",dic[@"issueNum"]];
        
        /*
         *  nums
         */
        historyCell.arrayNum=dic[@"result"];
        historyCell.boundaryNum = _boundaryNum;
        [historyCell createButtons];
    
    return historyCell;
}
//自定义cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        // Initialization code
        
        self.labelIssue=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
        self.labelIssue.text=@"期数";
        self.labelIssue.textColor=[UIColor blackColor];
        //        self.labelIssue.backgroundColor=[UIColor grayColor];
        self.labelIssue.font=[UIFont systemFontOfSize:15.f];
        [self.contentView addSubview:self.labelIssue];
        
        self.arrayBtn = [[NSMutableArray alloc] init];
    }
    
    return self;
}

-(void)createButtons
{
    //移除之前赋值的btn
    for (UIButton *button in self.arrayBtn) {
        [button removeFromSuperview];
    }
    [self.arrayBtn removeAllObjects];
    
    if ([self.arrayNum count]>0)
    {
        for (int i=0; i<[self.arrayNum count]; i++)
        {
            self.btnNum=[UIButton buttonWithType:UIButtonTypeCustom];
            self.btnNum.enabled=NO;
            self.btnNum.frame=CGRectMake(10+i*(BTNHEIGHT+2), 30, BTNWIDTH, BTNHEIGHT);
            [self.btnNum setBackgroundImage:[UIImage imageNamed:@"small_circle_light"] forState:UIControlStateNormal];
            [self.btnNum setTitle:[NSString stringWithFormat:@"%.2d",[[self.arrayNum objectAtIndex:i] intValue]] forState:UIControlStateNormal];

            //根据界面判断字体模式
            if ([_boundaryNum intValue] == 1) {
                <span style="color:#ff0000;">self.btnNum.titleLabel.textColor=[UIColor blueColor];</span>
                <span style="color:#3366ff;">[self.btnNum setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];</span>
                if ([[self.arrayNum objectAtIndex:i] integerValue] == 19||[[self.arrayNum objectAtIndex:i] integerValue] == 20) {
                    <span style="color:#3366ff;">[self.btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal];</span>
                    <span style="color:#ff0000;">self.btnNum.titleLabel.textColor=[UIColor redColor];</span>
                }
            }
            else if([_boundaryNum intValue] == 2){
                if (i < 6) {
                [self.btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                }
                else{
                     [self.btnNum setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                }
            }
            else if([_boundaryNum intValue] == 3){
               [self.btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
            
            [self.contentView addSubview:self.btnNum];
            [self.arrayBtn addObject:self.btnNum];
            
        }
    }
    
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值