标题未显示在UIButton上

时间:2011-07-20 16:36:18

标签: ios uibutton uibarbuttonitem

我使用以下代码在导航栏中显示带标题的自定义后退按钮。但是,标题没有显示,有什么帮助吗?

nb:后退按钮图像是黑色的,与ios默认使用的相同,需要自定义动作处理程序,这就是为什么这样做。

backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateSelected];    
[backButton addTarget:self action:@selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:@"Encameo" forState:UIControlStateNormal];
[backButton setTitle:@"Encameo" forState:UIControlStateSelected];

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];

1 个答案:

答案 0 :(得分:10)

您正在按钮上设置图像,而不需要设置UIButton的backgroundImage,然后您的标题将会显示。

像这样更改代码

backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateSelected];    
[backButton addTarget:self action:@selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:@"Encameo" forState:UIControlStateNormal];
[backButton setTitle:@"Encameo" forState:UIControlStateSelected];

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];