UIButtonTypeCustom 类型的 UIButton 将不显示标题 (iPhone) [英] UIButton of type UIButtonTypeCustom will not display Title (iPhone)

查看:15
本文介绍了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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆