0

我在导航控制器中有一个带有图像的后退按钮,我想将文本设置在图像顶部。我试过这个,但它似乎没有出现:

    UIImage *normalBackImage = [UIImage imageNamed:@"button_tl.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.bounds = CGRectMake(0, 0, normalBackImage.size.width, normalBackImage.size.height );
    [backButton setImage:normalBackImage forState:UIControlStateNormal];
    backButton.titleLabel.text = @"Back";

有谁知道如何做到这一点?谢谢!

4

2 回答 2

3
  1. 设置标题的颜色,否则你什么都看不到:)

    • setTitleColor:forState:

2. 不要尝试设置图像(即 FOREGROUND 图像)。而是设置背景图像:)

– setBackgroundImage:forState:

样本:

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 10, 100, 50);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitle:@"Bla" forState:UIControlStateNormal];

UIImage *normalBackImage = [UIImage imageNamed:@"button_tl.png"];
[backButton setBackgroundImage:normalBackImage forState:UIControlStateNormal];

[self.view addSubview:backButton];
于 2013-03-04T19:43:50.447 回答
0

您可以创建一个 UILabel 并将其作为子视图添加到您的 UIImage。看起来像这样(在编译器之外编写代码,可能并不完美)

UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:@"My Label Text"];
[image addSubview:myLabel];
于 2013-03-04T19:44:43.633 回答