[英]Custom button not changing text color when selected
我使用for循環創建了3個自定義按鈕。 但是當我選擇一個按鈕時,文本顏色沒有改變,該怎么辦? 我還要補充什么?
這是我的代碼
buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];
NSInteger numControllers = [viewControllerArray count];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
[button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
NSInteger numControllers = [viewControllerArray count];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
[button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
- (IBAction)tapButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
if ([btn isSelected]) {
[btn setSelected:NO];
[btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
}
else
{
[btn setSelected:YES];
[btn setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:24.0/255.0 blue:204.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
int x = 0;
for (int i = 0; i < [viewControllerArray count]; i++){
//int y=2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x+2, 0, 40, 36)];
id myArrayElement=[viewControllerArray objectAtIndex:i];
[button setTitle:[NSString stringWithFormat:@"%@",myArrayElement] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackcolor]];
button.titleLabel.font = [UIFont systemFontOfSize:15];
x += button.frame.size.width;
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
我已經嘗試過您的帶有虛擬文本的按鈕的代碼,正常狀態為黑色文本顏色,點擊時突出顯示按鈕顏色:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 40,50 , 35);
// button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
[button setTitle:@"Dummy" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
下面的行不會長時間更改按鈕文本的顏色,它會在點擊后幾秒鍾內更改按鈕的文本顏色
[按鈕setTitleColor:[UIColor colorWithRed:137.0 / 255.0綠色:110.0 / 255.0藍色:255.0 / 255.0 alpha:1] forState:UIControlStateHighlighted];
嘗試這個:
NSInteger numControllers = [viewControllerArray count];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER,(self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
button.tag=i+200;
[button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
- (IBAction)tapButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
[btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
for (int i = 0; i<numControllers; i++) {
UIButton *restBtn=[navigationView viewWithTag:i+200];
if(restbtn!= btn)
{
[restBtn setBackgroundColor:[UIColor grayColor] forState: UIControlStateNormal];
[restBtn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.