Программируем под IOS
Гость
Установить цвет заголовка не работает – UIButton
Сообщение
Гость » 14 мар 2024, 13:47
Я создал массив UIButtons с одинаковым действием:
если я нажму первую кнопку, цвет первой кнопки должен измениться, цвет другой кнопки не должен быть изменен. как этого добиться?
Код: Выделить всё
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Заранее спасибо
Источник:
https://stackoverflow.com/questions/392 ... g-uibutton
1710413257
Гость
Я создал массив UIButtons с одинаковым действием: если я нажму первую кнопку, цвет первой кнопки должен измениться, цвет другой кнопки не должен быть изменен. как этого добиться? [code]for(titleStr in titleArray){ actionButton = [UIButton buttonWithType:UIButtonTypeCustom]; [actionButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; actionButton.tag = count; [actionButton setTitle:titleArray[count] forState:UIControlStateNormal]; [actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize]; CGRect buttonframe; buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25); actionButton.frame = buttonframe; [titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]]; [contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]]; if(count==0){ actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0]; [actionButton setSelected:YES]; tempObj = actionButton; }else { actionButton.titleLabel.font=[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0]; } [titleScrollView addSubview:actionButton]; contentOffset += actionButton.frame.size.width+5.0; titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height); // Increase the count value count++; } -(void)buttonTapped:(id)sender { if([sender tag]==0){ UIButton *tappedButton = (UIButton *)sender; [actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal]; actionButton.titleLabel.textColor = [UIColor whiteColor]; [actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; [tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; } } [/code] Заранее спасибо Источник: [url]https://stackoverflow.com/questions/39270540/set-title-color-is-not-working-uibutton[/url]