UIButton setTitle颜色变化不起作用

时间:2017-11-07 06:51:04

标签: objective-c uiscrollview uibutton uicolor

我已经以编程方式创建了滚动视图并使用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];

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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


 }

答案 1 :(得分:0)

我创建了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];
}
  }