如何更改条形按钮项目颜色?

时间:2013-11-22 08:40:36

标签: ios objective-c colors uibarbuttonitem

在我的详细视图控制器(导航控制器应用程序的一部分)中,我添加了自定义“后退”按钮,如下所示:

- (void)loadView
{
    [super loadView];

    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];

    UIImage *backImage = [UIImage imageNamed:@"left_arrow_icon"];
    [backButton setBackgroundImage:backImage  forState:UIControlStateNormal];

    [backButton setTitle:@"" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    self.navigationItem.leftBarButtonItem = backButtonItem;
}

-(void) popBack {
    [self.navigationController popViewControllerAnimated:YES];
}

如您所见,我添加了左栏按钮并指定了“popBack”动作。基本上left_arrow_icon是银色的,但当我按下它时,iOS会将其更改为深灰色。

我的问题是,我(和如何)可以将初始颜色更改为白色吗?那可能吗?

编辑。我用xcode5

EDIT2: 那也行不通 enter image description here

2 个答案:

答案 0 :(得分:0)

试试这段代码 -

[backbutton setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
    [UIColor blackColor], UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

答案 1 :(得分:0)

试试这段代码。它对我来说很完美 -

UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage    imageNamed:@"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];

UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:@"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];