添加子视图后,iOS UIButton文本颜色不会更改

时间:2014-09-08 20:51:15

标签: ios objective-c uiview uibutton

我有这个功能,一旦点击一个按钮就会被调用。

我正在尝试为单击的按钮添加视图,并为未单击的按钮添加视图。

这是我的观点的初始值:

/**
 Create the bottom border views
 */
- (void) createBottomBorderViews {
    UIButton* button = _albumsTypesButtons[0];
    //create layers
    _clickedButtonBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 26, button.frame.size.width, 2)];
    _unClickedButtonBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 27, button.frame.size.width, 1)];

    //set the clicked layer
    _clickedButtonBorder.backgroundColor = [UIColor colorWithRed:86/255.0 green:88/255.0 blue:87/255.0 alpha:1.0];

    //set the unclicked layer
    _unClickedButtonBorder.backgroundColor = [UIColor colorWithRed:86/255.0 green:88/255.0 blue:87/255.0 alpha:1.0];
}

这是根据按钮的状态添加视图和更改文本颜色

-(void) setButtonsVisuals:(UIButton*)clickedButton {
    [_clickedButtonBorder removeFromSuperview];
    [_unClickedButtonBorder removeFromSuperview];
    for (UIButton* button in _albumsTypesButtons)
    {
        if (button == clickedButton)
        {
            button.titleLabel.textColor = [UIColor colorWithRed:225/255.0 green:112/255.0 blue:119/255.0 alpha:1.0];
            [button addSubview:_clickedButtonBorder];
        }
        else
        {
            button.titleLabel.textColor = [UIColor colorWithRed:199/255.0 green:200/255.0 blue:196/255.0 alpha:1.0];
            [button addSubview:_unClickedButtonBorder];
        }
    }
}

我的问题:

将视图作为子视图添加到按钮时,它不会更改     文字颜色......

我认为它可能是某些层或类似的东西,但它真的不知道

1 个答案:

答案 0 :(得分:1)

解决方案实际上是使用[button setTitleColor]而不是触摸它的属性。