将文本添加到自定义后退按钮iOS

时间:2013-03-04 19:39:27

标签: ios uinavigationcontroller

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

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

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

2 个答案:

答案 0 :(得分:3)

  1. 设置标题的颜色,否则你什么也看不见:)。

    • setTitleColor:forState:
  2. 2。 不要试图设置图像(这意味着FOREGROUND图像)。而是设置BACKGROUND图像:)

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

答案 1 :(得分:0)

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

UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:@"My Label Text"];
[image addSubview:myLabel];