UIButton setTitle 颜色更改不起作用

UIButton setTitle Colour Changing not working

我已经以编程方式创建了滚动视图并使用 for 循环创建了 N 个按钮,但现在我需要相应地更改 setTitle 颜色以选择或取消选择按钮

_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40,50,self.view.frame.size.width,40)];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.showsHorizontalScrollIndicator = NO;


for (int i = 0; i<_pagesNameArray.count; i++) {
    
   
    
  
    
    self.button = [[UIButton alloc]init];
    self.button.frame = CGRectMake(i*150, 0, 150, 40);
   
    
   
    self.button.tag = i;
    
    self.button.backgroundColor = [UIColor clearColor];
    [self.button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitle:[_pagesNameArray objectAtIndex:i] forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
  [_scrollView addSubview:self.button];
    
   
}
[self setupSelector];
CGRect contentRect = CGRectZero;
for (UIView *view in _scrollView.subviews) {
    contentRect = CGRectUnion(contentRect, view.frame);
}
_scrollView.contentSize = contentRect.size;

[self.view addSubview:_scrollView];

我也试过这个方法,但是不行。谁能帮帮我?

   [self.button setTitleColor:[UIColor grayColor]  forState:UIControlStateNormal];
   [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

-(void)tapSegmentButtonAction:(UIbutton *)sender
 {

 UIbutton *button=sender.tag;
 [self.button setTitleColor:[UIColor blackColor] 
          forState:UIControlStateSelected];


 }

我创建了 3 种不同类型的按钮并为每个按钮指定了标签

self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(0, 0, 180, 30);
self.button.tag = 0;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:@"LOCAL REPORTS" forState:UIControlStateNormal];
 // [self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont fontWithName:@"Oswald-Regular" size:20.0f];

[_scrollView addSubview:self.button];


self.buttonNews = [[UIButton alloc]init];
self.buttonNews.frame = CGRectMake(135, 0, 180, 30);
self.buttonNews.tag = 1;
self.buttonNews.backgroundColor = [UIColor clearColor];
[self.buttonNews addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonNews setTitle:@"NEWS" forState:UIControlStateNormal];
//[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonNews.titleLabel.font = [UIFont fontWithName:@"Oswald-Regular" size:20.0f];
self.buttonNews.titleLabel.font = [UIFont fontWithName:@"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonNews];



self.buttonTop = [[UIButton alloc]init];
self.buttonTop.frame = CGRectMake(135*2, 0, 180, 30);
self.buttonTop.tag = 2;
self.buttonTop.backgroundColor = [UIColor clearColor];
[self.buttonTop addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonTop setTitle:@"TOP PHOTOS" forState:UIControlStateNormal];
//[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
 self.buttonTop.titleLabel.font = [UIFont fontWithName:@"Oswald-Regular" size:20.0f];
self.buttonTop.titleLabel.font = [UIFont fontWithName:@"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonTop];
[self animateButton];



  -(void) animateButton
   {
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

if (self.currentIndex == 0) {
    [self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
    [self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

}
if (self.currentIndex == 1) {
    [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonNews setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
    [self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 2) {
    [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonTop setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
}
  }