【问题标题】:How to change title color of a set of UIButtons created programmatically?如何更改以编程方式创建的一组 UIButton 的标题颜色?
【发布时间】:2015-10-13 06:31:40
【问题描述】:

在我的应用程序中,我使用 for 循环以编程方式创建了几个按钮,如下所示。这是一个水平选项卡菜单

动作中,我必须突出显示选定的按钮(并将其余按钮标题变灰)。如何做到这一点? 它应该看起来几乎像下面的图像。

当一个按钮被点击时,被点击的按钮标题颜色应该是 白色和所有其他按钮都应该是灰色的。我知道我可以 在按钮操作中访问 sender.titlecolor。但是其他呢 按钮?

 -(void)createButtons
 {
     float buttonMinX=10;
     float  buttonMinY=0;
     scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _tabViewBackground.frame.size.width, _tabViewBackground.frame.size.height)];
     ButtonIndex=0;
     scrollView.showsHorizontalScrollIndicator = NO;
     [_tabViewBackground addSubview:scrollView];

     for (int i=0; i<_tabItemsListArray.count; i++)
     {
         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         button.tintColor=[UIColor whiteColor];
         button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
         button.titleLabel.numberOfLines = 1;
         button.tag=i;
         [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
         UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0f];
         NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
         [style setAlignment:NSTextAlignmentCenter];
         NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                                    NSFontAttributeName:font1,
                                    NSParagraphStyleAttributeName:style};
         NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
         [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[_tabItemsListArray objectAtIndex:i]  attributes:dict1]];
         [button setAttributedTitle:attString forState:UIControlStateNormal];


         buttonWidth= [self getWidthOfRect:button.titleLabel];
         button.frame = CGRectMake(buttonMinX,buttonMinY,buttonWidth,_tabViewBackground.frame.size.height);
         buttonMinX+=buttonWidth+05;

         sublayer = [[UIView alloc]init];
         sublayer.backgroundColor = [UIColor greenColor];
         sublayer.tag=kButtonSelectrorTag+i;
         sublayer.frame = CGRectMake(button.frame.origin.x,button.frame.size.height-2, button.frame.size.width,2);
         [scrollView addSubview:sublayer];

         sublayer.hidden=YES;
         if (ButtonIndex==i) 
         {
             sublayer.hidden=NO;
         }

         button.backgroundColor=[UIColor clearColor];
         [scrollView addSubview:button];
    }
    scrollView.backgroundColor = [UIColor blueColor];

    scrollView.contentSize = CGSizeMake(buttonMinX+10,_tabViewBackground.frame.size.height);
}

-(CGFloat)getWidthOfRect:(UILabel*)titleLabel
{
    CGFloat widthIs =[titleLabel.text boundingRectWithSize:titleLabel.frame.size options:NSStringDrawingUsesDeviceMetrics attributes:@{ NSFontAttributeName:titleLabel.font }context:nil].size.width;
        widthIs = ceilf(widthIs);
    //  NSLog(@"the width of yourLabel is %f", widthIs);
    return widthIs+30;
}

- (void)action:(UIButton*)sender
{
    for (int i=0; i<_tabItemsListArray.count; i++) 
    {
        UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+i];
        [tabSelector setHidden:YES];
    }
    UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+sender.tag];
    [tabSelector setHidden:NO];
}

我在每个按钮下方都有一个按钮选择器。我应该一次显示一个按钮选择器。使用 action: 中的代码效果很好

【问题讨论】:

  • 它不是重复的。查看编辑。单击按钮时,单击的按钮标题颜色应为白色,所有其他按钮应为灰色。我知道我可以访问 sender.titlecolor按钮操作。但是其他按钮呢?
  • 对于普通程序员来说,访问其他按钮也不应该太难。有几种方法可以做到这一点,其中一种方法是制作这些按钮的插座并在操作方法中使用它们。
  • 创建网点?我不明白你的逻辑。如何为以编程方式创建的 uibutton 创建网点?
  • 啊我错过了,如果您以编程方式创建了按钮,您可以将它们的引用保存在一个数组(一个实例变量)中,以后可以访问该数组以用于操作方法。

标签: ios objective-c uibutton


【解决方案1】:

我注意到您使用的是setAttributedTitle:forState:。您可以以相同的方式设置标题的属性,但也可以为 UIControlStateSelected 设置属性。

然后,如果您从UIControlStateSelected 设置button.selected = YES; 属性将适用。 如果您从UIControlStateNormal 设置button.selected = NO; 属性将适用。

编辑:

您可以像这样创建按钮:

NSInteger numberOfButtons = 10;
NSMutableArray *menuButtonsMutableArray = [[NSMutableArray alloc] initWithCapacity:numberOfButtons];
for (int i = 0; i < numberOfButtons; i++) {
    UIButton *button = [UIButton new];
    //layout your button somehow
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(menuButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

    [menuButtonsMutableArray addObject:button];
}

self.menuButtonsArray = [menuButtonsMutableArray copy];

然后在action方法中:

- (void)menuButtonDidTap:(UIButton *)sender {
    for (UIButton *button in self.menuButtonsArray) {
        button.selected = (button == sender);
    }
}

【讨论】:

  • 单击按钮时,单击的按钮标题颜色应为白色,所有其他按钮应为灰色。我知道我可以在按钮操作中访问 sender.titlecolor。但是其他呢按钮?我已经创建了 5 个按钮。我应该如何应用你的逻辑?我不明白
  • @abhi1992 首先,我会考虑子类化UISegmentedControl。这个控件似乎很适合你需要安静的好。如果您想继续使用您的解决方案,则必须将所有菜单按钮存储在 NSArray 中,然后如果用户点击此按钮中的任何一个,您将必须遍历数组并设置所有按钮的状态。
【解决方案2】:
@IBOutlet var AllButtons: [UIButton]!
for button in AllButtons {
    if button.backgroundColor == UIColor.red {
        button.backgroundColor = compare.backgroundColor
    }
}

将所有按钮拖放到AllButtons 中,然后使用for 循环访问所有按钮并做任何你想做的事情。
这是一个简单的例子,希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2012-09-28
    • 2014-11-07
    • 2011-01-29
    • 2014-08-22
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多