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

Ric*_*kiG 40 iphone uibutton

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

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

    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];
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢额外的一双眼睛:)

Tuo*_*nen 90

图像覆盖标题,您需要使图像成为背景图像以显示标题.

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


小智 28

我只是骗了spome非常相似的问题...只是设置标题颜色,我猜当前的一个是白色;)

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

  • 有时你甚至不得不使用setTitleColor而不是uibutton.titleLabel.textColor (3认同)

Jap*_*Con 13

我发现了问题!

这不是图像覆盖标题.正在被图像推离帧.

试试这个:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -img.size.width, 0.0, 0.0 )];

希望能帮助到你!