UIButton 上不显示标题

Posted

技术标签:

【中文标题】UIButton 上不显示标题【英文标题】:title not displayed on UIButton 【发布时间】:2011-07-20 16:36:17 【问题描述】:

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

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];

【问题讨论】:

看看这个。 ***.com/questions/2681321/… 【参考方案1】:

您在按钮上设置图像而不是这个,您需要设置 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];

【讨论】:

如果你在正常状态和选中状态下使用相同的标题颜色和标题,那么声明正常状态就足够了

以上是关于UIButton 上不显示标题的主要内容,如果未能解决你的问题,请参考以下文章

UIButton 在 UIWindow 的子视图上不起作用

界面生成器 UIButton 自定义背景图像在模拟器/设备上不起作用

UIButton 标题不显示

UiButton 点击​​在 UIView 上不起作用

iOS:UIButton 不显示

tabBar 上不显示图标