iOS8 UIButton标题消失

时间:2014-09-19 12:24:00

标签: objective-c uibutton uilabel ios8

我的应用程序中有一个按钮,在iOS7上完全可见。但是当我切换到iOS8时,不会出现此按钮的标题标签。我只能看到按钮周围的边框。 这是制作按钮的代码。有人能给我一个问题所在的线索吗?

UIButton *problem = [[UIButton alloc] initWithFrame:frame];
[problem addTarget:self action:@selector(problemButton:) forControlEvents:UIControlEventTouchUpInside];
problem.backgroundColor = [UIColor whiteColor];
[problem setTitle:NSLocalizedString(@"ReportAProblem", nil) forState:UIControlStateNormal];
problem.titleLabel.textColor = CUSTOM_DARK_GRAY_COLOR;
[[problem layer] setBorderWidth:1.0f];
[[problem layer] setBorderColor:CUSTOM_DARK_GRAY_COLOR.CGColor];
problem.layer.cornerRadius = 5;
problem.clipsToBounds = YES;

我找到了这个问题的答案,但立即有另一个问题: 我的一些UILabel遇到了同样的问题,例如:

 self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.numberOfLines = 1;
self.label.adjustsFontSizeToFitWidth = YES;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor = self.textColor;
CGFloat fontSize = self.bounds.size.height / 5;
self.label.font = [UIFont systemFontOfSize:fontSize];

1 个答案:

答案 0 :(得分:3)

您应该使用- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state代替titleLabel.textColor

第二个问题:为什么要尝试将视图高度的比例设置为字体大小?也许用你的标签做这件事更有意义。 CGFloat fontSize = self.label.bounds.size.height / 5;无论如何,您通常想要做的是使用固定的字体大小并调整其周围的标签框架。