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

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

问题描述

我一定忽略了一些完全明显的东西?但我的按钮正确显示其图像和大小,但我根本无法显示标题。

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

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

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天全站免登陆