【问题标题】:UIbarbutton blue instead of background imageUIbarbutton 蓝色而不是背景图片
【发布时间】:2013-12-30 15:53:37
【问题描述】:

我正在尝试使用自定义 png 文件为背景添加后退按钮,但每次我使用情节提要添加背景时,它都会变成这样的蓝色:

如何在 UIbarbutton 上添加背景图片?

后退按钮如下所示:

【问题讨论】:

    标签: ios objective-c uibutton uinavigationbar uibarbuttonitem


    【解决方案1】:

    这是 iOS 7 中按钮中图像的标准行为。图像被渲染为模板图像,不透明区域以当前色调着色,透明区域为透明。如果要查看图像,需要使用 imageWithRenderingMode: 创建图像,并将 UIImageRenderingModeAlwaysOriginal 作为参数传递。

    【讨论】:

      【解决方案2】:

      你需要在语法上做到这一点。 我已经尝试在情节提要中这样做,看起来有一个非常奇怪的错误,导致开发人员决定 - 使用文本或使用图像,而不是两者....

      - (void)viewDidLoad
      {
           [super viewDidLoad];
      
           [self addButtonsToNavigationBar];
      
      }
      
      
      - (void)addButtonsToNavigationBar
      {
           UIButton *regularButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 100.0f, 30.0f)];
           UIImage *historyButtonImage = [UIImage imageNamed:@"image_name.png"];
            // can set the background color instead of setting an image. 
           [regularButton setBackgroundImage:historyButtonImage forState:UIControlStateNormal];
      
           [regularButton setTitle:@"Some button name" forState:UIControlStateNormal];
           [regularButton addTarget:self action:@selector(historyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
           UIBarButtonItem *navigationBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:regularButton];
           self.navigationItem.leftBarButtonItem = navigationBarButtonItem;
      }
      

      【讨论】:

      • 我已经尝试过您的代码,但没有显示任何内容。真的很奇怪。
      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多