簡體   English   中英

UIButton中的自定義UIBarButtonItem的標題未顯示

[英]Title of a Custom UIBarButtonItem from a UIButton not showing

我從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;

您應該為使用setTitle:forState:按鈕設置標題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;

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM