ios - UIButton 自定义按钮不会在选择时更改字体颜色

标签 ios xcode cocoa-touch uibutton

我正在使用这段代码创建一个 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];

最佳答案

将您想要的背景颜色放在 forState:Highlighted 下。

当它被按下时,它不在 UIControlState NormalUIControlState Highlighted 中。

关于ios - UIButton 自定义按钮不会在选择时更改字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537955/

相关文章:

ios - 为 ARKit 请求 3D 模型的指南

ios - 使用结构在 Swift 5 中使用 JSONDecoder 解析 JSON

iphone - 如何在最终应用程序中包含所有内容

iphone - 稍微放大 MKCoordinateRegion?

iOS App 包含开发者路径信息

ios - UITableViewCell 子类 : Delete and reorder button not showing

ios - 自定义表格 View 的 ScrollView

ios - 核心数据 - NSFetchedResultController 按其他表字段对数据进行排序

ios - UIStackView : Change UIStackView image width using storyboard

iphone - 是否可以在没有按钮的情况下切换 View ?