我正在使用此代码创建UIButton,但是当按下按钮时,字体不会改变颜色。
_gameSlider = [UIButton buttonWithType:UIButtonTypeCustom];
[_gameSlider setBackgroundImage:[[UIImage imageNamed:@"sliderBackgroundSticker"] resizableImageWithCapInsets:UIEdgeInsetsMake(22, 16, 22, 16)] forState:UIControlStateNormal];
_gameSlider.frame = CGRectMake(0, 0, self.view.frame.size.width - 20, 70);
newCnt = self.view.center;
newCnt.y = newCnt.y + 120;
_gameSlider.center = newCnt;
[_gameSlider setTitle:@"PLAY" forState:UIControlStateNormal];
[_gameSlider.titleLabel setFont:[UIFont fontWithName:@"Chalkduster" size:22]];
[_gameSlider setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[_gameSlider setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected | UIControlStateHighlighted];
[_gameSlider addTarget:self action:@selector(playButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_gameSlider];
答案 0 :(得分:2)
将您想要的背景颜色放在forState:突出显示。
当它被按下时,它不在UIControlStateNormal
UIControlStateHighlighted
。