在自定义按钮上设置文本

时间:2013-07-31 01:28:53

标签: iphone objective-c uibutton

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

- (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
}

但它仍然看起来像这样

enter image description here

2 个答案:

答案 0 :(得分:1)

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

[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];

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

答案 1 :(得分:0)

尝试backButton.text = @"DONE"

修改:尝试更改此行:
[backButton setTitle:@"DONE" forState:UIControlStateNormal];

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