UIButtonTypeCustom 类型的 UIButton 将不显示标题 (iPhone)

UIButton of type UIButtonTypeCustom will not display Title (iPhone)(UIButtonTypeCustom 类型的 UIButton 将不显示标题 (iPhone))
本文介绍了UIButtonTypeCustom 类型的 UIButton 将不显示标题 (iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定忽略了一些非常明显的东西??但是我的按钮正确显示了它的图像和大小,但我根本无法显示标题.

我做了一个非常简单的测试,当我这样做时,标题甚至没有出现:

 CGRect frameBtn = CGRectMake(160.0f, 150.0f, 144.0f, 42.0f);UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[按钮 setImage:[UIImage imageNamed:@"left_halfscreen_button.png"] forState:UIControlStateNormal];[按钮 setTitle:@"Hello" forState:UIControlStateNormal];[按钮 setFrame:frameBtn];NSLog(@"Title:%@", [button currentTitle]);//打印标题:你好[自我添加子视图:按钮];

我有一个为我生成自定义按钮的工厂类,我认为我在那里搞砸了一些细节,所以我将上面的代码直接移动到我的 UIView 中,标题仍然是空白的.

这是一个错误还是我只是错过了眼前的一些东西.

谢谢你的眼睛:)

解决方案

图片覆盖标题,需要将图片设为背景图片才能显示标题.

<上一页>[按钮setBackgroundImage:[UIImage imageNamed:@left_halfscreen_button.png"]forState:UIControlStateNormal];

I must have overlooked something completely obvious?? but my button displays its image and size correctly, but I simply can't get the Title to show up.

I did a really simple test, the Title does not even show up when I do this:

    CGRect frameBtn = CGRectMake(160.0f, 150.0f, 144.0f, 42.0f);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"left_halfscreen_button.png"] forState:UIControlStateNormal];
    [button setTitle:@"Hello" forState:UIControlStateNormal];
    [button setFrame:frameBtn];
    NSLog(@"Title:%@", [button currentTitle]);
    //prints "Title:Hello
    [self addSubview:button];

I have a factory class that generates custom buttons for me and I thought I messed some detail up there, so I moved the above code directly into my UIView, the title is still blank.

Is this a bug or am I simply missing something right in front of my eyes.

Thank you for the extra set of eyes:)

解决方案

Image overrides title, you need to make the image a background image to show the title.

[button setBackgroundImage:[UIImage imageNamed:@"left_halfscreen_button.png"] 
        forState:UIControlStateNormal];

这篇关于UIButtonTypeCustom 类型的 UIButton 将不显示标题 (iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
HTTPS request using volley(使用 volley 的 HTTPS 请求)