IOS 7 UIButtonBarItem Image Does Not Shade

In my navigation bar, I have a pair of rightBarButtonItems that have custom icons (the icon images are white, which works well with the iOS 6 base color scheme).

In iOS 7, loading images using initWithTitle (see code snippet 1) replaces the β€œwhite” color in the icon with the corresponding global hue (in this case, the specific color is blue)

Code Snippet 1 :

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStyle) UIBarButtonSystemItemCancel target:(self) action:@selector(refreshList)]; refreshButton.image = [UIImage imageNamed:@"RefreshIcon.png"]; 

However, I needed to use initWithCustomView to overcome strange changes in behavior that caused the icons to get out of sight. The main idea was to set the size of the icons. initWithCustomView solved the calibration problem, but does not display button images with a global hue, they are displayed in the image color (white). Code Snippet 2 shows how I create a button with initWithCustomView.

Code Snippet 2 :

 CGRect frameCustomButton2 = CGRectMake(0.0, 0.0, 18.0, 18.0); UIButton *customButton2 = [[UIButton alloc] initWithFrame:frameCustomButton2]; [customButton2 setBackgroundImage:iconRefreshButton forState:UIControlStateNormal]; UIBarButtonItem *barCustomButton2 =[[UIBarButtonItem alloc] initWithCustomView:customButton2 ]; barCustomButton2.image = iconRefreshButton; [customButton2 addTarget:self action:@selector(refreshList) forControlEvents:UIControlEventTouchUpInside]; 

All this code, of course, is in the (void) viewDidLoad. I tried things like:

 barCustomButton2.tintColor = [UIColor blackColor]; //doesn't work 

or [barButtonAppearance setTintColor: [UIColor blackColor]]; // does not work

and they do not override the white color of the image. Does this look like creating a custom view after viewing in global hue color?

How can I make sure the button icon is global?

Thanks!

+7
ios objective-c iphone ios7 uibuttonbaritem
source share
2 answers

I just wanted to get it in the root comment in order to better use the context for the checkmark "answer" and to format it better.

I could figure it out! You can specify that the image will always be displayed as a template, which will force it to take the global color of the shades.

 UIImage *iconRefreshButton = [UIImage imageNamed:@"MyIconFilename.png"]; iconRefreshButton = [iconRefreshButton imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

By default, if you did not specify it, it is "UIImageRenderingModeAutomatic", which means that it will be displayed as a template or source image based on context.

+19
source share

You will either have to get around the problem you have with the first code snippet, or you will have to create a subclass of UIButton that uses its image as a mask to show the hue color in drawRect:

I would recommend the first approach.

+1
source share

All Articles