选择时,自定义按钮不会更改文本颜色

时间:2017-11-30 09:52:09

标签: ios objective-c uibutton uicontrolstate

我使用for循环创建了3个自定义按钮。但是当我选择一个按钮时,文字颜色没有变化。我该怎么做?我还要添加什么?

这是我的代码

buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];    
NSInteger numControllers = [viewControllerArray count];

for (int i = 0; i<numControllers; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
    [button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:button];
}

1 个答案:

答案 0 :(得分:0)

    NSInteger numControllers = [viewControllerArray count];

    for (int i = 0; i<numControllers; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
        [button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [navigationView addSubview:button];
    }
- (IBAction)tapButtonAction:(id)sender
{

    UIButton *btn = (UIButton*)sender;
    if ([btn isSelected]) {
        [btn setSelected:NO];
        [btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];

    }
    else
    {
        [btn setSelected:YES];
        [btn setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:24.0/255.0 blue:204.0/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    }
}

使用此代码

更改for循环
int x = 0;
    for (int i = 0; i < [viewControllerArray count]; i++){
        //int y=2;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x+2, 0, 40, 36)];

        id myArrayElement=[viewControllerArray objectAtIndex:i];

        [button setTitle:[NSString stringWithFormat:@"%@",myArrayElement] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackcolor]];

        button.titleLabel.font = [UIFont systemFontOfSize:15];


        x += button.frame.size.width;

        [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }