【问题标题】:how to change the color of backBarButtonItem?如何更改 backBarButtonItem 的颜色?
【发布时间】:2011-09-21 09:29:51
【问题描述】:

在我的应用程序中,我想更改 bacBarButtonItem 的颜色。 可以改变颜色吗?或者我必须把它的图像。 如果是图像,请告诉我如何放置图像的代码。

【问题讨论】:

    标签: iphone xcode backbarbuttonitem


    【解决方案1】:

    如果你只是想改变颜色,你可以用这行代码来完成。

    [[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
    

    将 redColor 替换为以下内容以调整按钮的颜色:

    colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this
    

    一定要把它放在推送的视图控制器中。不是您希望看到此后退按钮颜色的视图控制器。

    【讨论】:

    • “一定要把它放在推送的视图控制器中。不是你想看到这个后退按钮颜色的视图控制器。”非常重要,并且澄清了很多事情。
    • @kkendall:如果我只想为后栏按钮(设置为默认系统生成的栏按钮)而不是所有栏按钮项目设置色调颜色怎么办?
    【解决方案2】:

    Praveen-K 的答案是正确的,但请记住,您必须在每个视图控制器中都这样做。

    从 iOS5 开始,Apple 引入了“外观”的概念。

    - (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
    

    在你的情况下是这样的

    UIImage *image = [UIImage imageNamed:@"imageName.png"];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    

    但正如我所说,Praveen-K 的答案是好的并且会起作用,但只是为了让你知道未来。

    【讨论】:

      【解决方案3】:
      UIImage *image = [UIImage imageNamed:@"imageName.png"];
      UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backButtonAction)];
      self.navigationItem.leftBarButtonItem=backBarButton;
      [backBarButton release];
      

      【讨论】:

        【解决方案4】:

        另一种改变后栏按钮项颜色的方法是使用段控制

        UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", nil]] autorelease];
        button.frame = CGRectMake(0, 0, 60, 30);
        button.center = self.view.center;
        button.momentary = YES;
        button.segmentedControlStyle = UISegmentedControlStyleBar;
        button.tintColor = [UIColor colorWithRed:0 green:0.1 blue:0.5 alpha:0];
        [button addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventValueChanged];
        
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        

        请注意,我们将我们想要的颜色分配给 UISegmentedControl 的属性 tintColor。 我从这个网站得到了这个想法: http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-27
          • 1970-01-01
          • 1970-01-01
          • 2020-09-16
          • 2011-03-23
          • 2011-01-02
          • 2012-10-16
          相关资源
          最近更新 更多