【问题标题】:How to change Bar Button Item colour?如何更改条形按钮项目颜色?
【发布时间】:2013-11-22 08:40:36
【问题描述】:

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

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

编辑2: 这也行不通

【问题讨论】:

标签: ios objective-c colors uibarbuttonitem


【解决方案1】:

试试这个代码。它非常适合我 -

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

【讨论】:

  • 为什么要声明 UIImageView?你能粘贴你的 loadView 方法吗?
  • 其实这里我要创建自定义的navigationBar,那个image view就是navigationBar的图片。
  • 我只需要向导航栏添加按钮。我想在左上角有我自己的 png 图标。 png 文件中的箭头是银色的。使用上面显示的代码,我可以看到我的箭头,“返回”的操作也可以......但我希望这个图标是纯白色的。一定有办法,因为 Xcode 或“某事”将这种浅银色更改为深灰色(按下按钮时):/
【解决方案2】:

试试这个代码 -

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

【讨论】:

  • 你试过了吗? [.... NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,......]
  • 嗯,你写了后退按钮(在我的例子中是 UIButton)。 UIButton 没有 setTitleTextAttributes 方法。所以我假设,你的意思是 backButtonItem。所以我做了(在我有问题的edit2上掠夺)..它没有用
猜你喜欢
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多