来自未显示的UIButton的自定义UIBarButtonItem的标题

时间:2014-07-10 21:41:53

标签: ios uibutton uibarbuttonitem

我从UIBarButtonItem制作了自定义UIButton。自定义栏按钮可见,动作正常,大小正确,可以看到背景颜色;但是无法看到手动设置的标题。也许背景是在文本上?我不确定,帮忙吗?

viewDidLoad

UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:@selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:@"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = @"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;

UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];

self.navigationItem.rightBarButtonItem = resetBarBTN;

2 个答案:

答案 0 :(得分:5)

您应为特定控件状态设置使用setTitle:forState:按钮的标题,用下面的代码替换

UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:@selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:@"ProximaNova-Bold" size:19];

[resetButton setTitle:@"RESET" forState:UIControlStateNormal]; //change this line in your code

resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;

UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];

self.navigationItem.rightBarButtonItem = resetBarBTN;

答案 1 :(得分:0)

使用setTitle:forState设置按钮标题。例如

[resetButton setTitle:@"text" forState:UIControlStateNormal]