0

我用自定义图像制作了一个自定义按钮。然后我尝试设置它的标题,但它没有工作。这是按钮的代码

- (void)viewDidLoad
{
    UIImage *backButtonImage = [UIImage imageNamed:@"button.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:backButtonImage forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor colorWithRed:0.49 green:0 blue:0 alpha:1] forState:UIControlStateNormal];
    [backButton setTitle:@"DONE" forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
    [backButton addTarget:self action:@selector(goBackOne) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.hidesBackButton = YES;//hide original back button
}

但它仍然看起来像这样

在此处输入图像描述

4

2 回答 2

1

如果您希望图像出现在文本下方,则应使用 backgroundImage 属性:

[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];

如果您使用 image 属性,图像会将文本推开(当您想并排显示小图像和文本时,它可以工作)

于 2013-07-31T02:30:18.003 回答
0

试试backButton.text = @"DONE"

编辑:尝试改变这一行
[backButton setTitle:@"DONE" forState:UIControlStateNormal];

[backButton setText:@"DONE" forState:UIControlStateNormal];

于 2013-07-31T02:08:15.730 回答