【发布时间】: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