2014-07-10 91 views
1

我從UIButton自定義了UIBarButtonItem。自定義欄按鈕是可見的,動作有效,大小正確,並且可以看到背景色;然而手動設置的標題不能被看到。也許背景是放置在文字上?我不確定,幫忙?來自UIButton的自定義UIBarButtonItem的標題不顯示

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; 

回答

4

你應該設置標題使用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; 
+0

它的工作取代你的代碼,謝謝! – momodude22

0

使用的setTitle:forState設置按鈕標題。例如

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