i'm trying to add a back button using a custom png file for the background, but every time i add the background using the storyboard it just become blue like this:
How can i add a background image on a UIbarbutton?
the back button look like this:
This is the standard behavior in iOS 7 for an image in a button. The image is rendered as a template image, with opaque areas colored the current tint color, and transparent areas, transparent. If you want to see the image, you need to create the image with imageWithRenderingMode: and pass UIImageRenderingModeAlwaysOriginal as the argument.
You will need to do it grammatically.
I have tried doing it in storyboard, and it looks like there is a really strange bug, that causes the developer to decide - either use text or use an image, not both....
- (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;
}
Related
I have buttons in my costoum cell , and i'm setting the images from code . But something strange is happening. My images are blue .
BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]];
if (isTheObjectThere==TRUE) {
cell.favBtn.hidden = NO;
[cell.favBtn setImage:[UIImage imageNamed:#"favorite_star.png"] forState:UIControlStateNormal];
cell.favBtn.tag = indexPath.row;
[cell.favBtn addTarget:self action:#selector(unfavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
As mentioned in the comments, you should create the button with a UIButtonTypeCustom type in case it's currently set to UIButtonTypeSystem and you want to avoid the tint color from taking over. Alternatively, you can set the image rendering mode to make sure you always get the original image and not a tinted one:
[[UIImage imageNamed:#"favorite_star.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
On my nav bar, I have a couple of rightBarButtonItems that have custom icons (the icon images are white, which worked well with the basic color scheme of iOS 6).
Under iOS 7, loading the images using initWithTitle (see code snippet 1) replaces the "white" color in the icon with the proper global tint (a specific color of dark blue in this case)
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 a weird change in behavior that was causing the icons to move out of view. The basic idea was to specifically set the size of the icons. initWithCustomView solved the sizing problem, but does not display the button images with the global tint, they are displayed in the color of the image (white). Code Snippet 2 shows how I am creating the 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 of this code is of course in (void)viewDidLoad. I have tried things like:
barCustomButton2.tintColor = [UIColor blackColor]; //doesn't work
or
[barButtonAppearance setTintColor:[UIColor blackColor]]; // doesn't work
and they do not override the white color of the image. It is almost as if the creation of the custom view takes place after the view looks at the global tint color?
How can I ensure the button icon takes on the global tint?
Thanks!
Just wanted to get this into a root comment to give better context to the "answer" checkmark, and give better formatting.
I was able to figure this one out! You can tell the image to always render as template, which will force it to take on the global tint color.
UIImage *iconRefreshButton = [UIImage imageNamed:#"MyIconFilename.png"];
iconRefreshButton = [iconRefreshButton imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
The default, if you don't set it, is "UIImageRenderingModeAutomatic" which means it will render as a template or original image based on context.
You'll either have to work around the issue you were having with the first code snippet, or you'll have to create a UIButton subclass that uses its image as a mask to show the tint color in drawRect:.
I'd recommend the first approach.
I am working on an iPad UI and a button on that UI needs to have this image:
http://imgur.com/tVkP8wd
(as a PNG). The button is being declared like this:
CGRect newNoteButtonRect = CGRectMake(0, 0, 69, 43);
UIButton* newNoteButton = [[UIButton alloc] initWithFrame:newNoteButtonRect];
newNoteButton.imageView.contentMode = UIViewContentModeScaleToFill;
[newNoteButton setImage:self.fNewNoteIcon forState:UIControlStateNormal];
where 'fNewNoteIcon' is a UIImage. When the UI comes up, the image is tiny and squished, and almost nothing I do can change that. Any ideas?
The icon is initialized like this:
self.fNewNoteIcon = [UIImage imageNamed:#"New_Note.png"];
In that provided code you haven't specified a UIButton type? Creating a button with a custom image
// Create image
self.fNewNoteIcon = [UIImage imageNamed:#"buttomImage.png"];
// Create rect for button. 0, 0 and size it from image
CGRect newNoteButtonRect = CGRectMake:(0, 0, _fNewNoteIcon.size.width, _fNewNoteIcon.size.height);
// Alloc and init UIButton with type
// Pass in image. Add to subview of view.
UIButton *newNoteButton = [UIButton buttonWithType: UIButtonTypeCustom];
[newNoteButton setImage: _fNewNoteIcon forState: UIControlStateNormal];
[self.view addSubview: newNoteButton];
I type all of this out in SO without compiling so check over but that should get you a UIButton, with the required image at the correct size.
So it looks like if your PNG doesn't have an Alpha channel, then you'll have size issues. I opened the PNG in Photoshop and added an Alpha channel. The image is now properly sized in the iPad!
I have created a custom button using a .png image that has transparency. However, when I implement it as a UIImage, the transparency is lost. Here's the code that I'm using:
- (void)setMyCustomBackButton;
{
UIImage *backButtonImage = [UIImage imageNamed:#"Back Button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popCurrentViewController) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -12, -2);
[backButtonView addSubview:backButton];
UIBarButtonItem *finalBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = finalBackButton;
}
Is there a UIImage property for transparency? Does it have to do with opacity?
If you're creating the PNG in an image editing software, please make sure you've encoded the transparency while creating the image. Sometimes some image editors have an option for 'saving transparency' that's unchecked by default, when you're exporting something as a PNG.
Other than that, any transparency on a PNG will show up on a UIButtonTypeCustom. You don't need to do anything special to preserve transparency on a UIImage that's loading up a PNG.
Before you use the image in the code, please open it in Preview to make sure the transparency is present.
Oh and to answer your other question, UIImage does not have any properties relating to transparency. The closest thing you have is the alpha property for a UIView but even that simply changes the overall opacity of your UIView.
EDIT: Missed this the first time I read your question. Try:
UIBarButtonItem *finalBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
No need to have the backButtonView. You can pass in the UIButton as the custom view. I use this to create custom bar button items. This should fix your problem.
EDIT 2: The UIBarButtonItem class does not have an alpha property you can set. Also, modifying the alpha property of the UIButton that you set as the UIBarButtonItem's custom view won't affect the transparency of the UIBarButtonItem.
The only way you can do this is to modify the transparency of the source image being used for the UIButton.
Modify your original PNG to have the desired transparency you want (and based on your comments, you need a very low transparency). Use an image editing software / preview to fine tune the transparency before you export the PNG.
I have run into a strange issue. I have a UIButton with UIButtonTypeCustom.
For it's background image, I am using a transparent image. The issue is that the transparency on the actual image doesn't seem to be correct. The odd thing is that it is in fact transparent, because the background shows correctly behind the button.
Below is an example of what the button looks like (left) and what the button should look like (right). I took a screenshot and overlaid the image on the background in Photoshop, and the background shows correctly inside the image, while in the actual button on the left it does not. Noticeably, the glow is more intense on the left UIButton vs. the actual image when inserted onto the background.
Here's the image I am using to show that it does in fact have transparency:
Here's my code:
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.backgroundColor = [UIColor clearColor];
nextButton.frame = CGRectMake(0, 0, 30, 30);
[nextButton setBackgroundImage:[[UIImage imageNamed:#"ButtonBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
forState:UIControlStateHighlighted];
[self addSubview:nextButton];
I have used the exact same image elsewhere to draw with and had no issue with transparency.
UPDATE: Adding other transparent images similarly increases the intensity of the alpha. While they're transparent, they seem darker and therefore less transparent. Again, works perfect elsewhere.
UPDATE 2: Even worse, I just created a new project with the exact same image dragged from the other project, created a button and had no issues with the button displaying correctly. How incredibly annoying!
You shouldn't have to set the background color for it to be transparent. Also, try removing the stretchable call on the image.
Also, you should be setting the image, not the background image.
If none of that helps, then Apple may just be improperly rendering your image. Try creating a CALayer and set its contents to your image and see if that works properly.
try this
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.frame = CGRectMake(0, 0, 30, 30);
nextButton.alpha=0.5f;
[nextButton setBackgroundColor:[UIColor clearColor]];
[nextButton setOpaque:NO];
[nextButton setBackgroundImage:[UIImage imageNamed:#"ButtonBackground.png"]
forState:UIControlStateNormal];