Blue image for buttons - ios

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]

Related

How to change bar button image color?

I want to set image in bar button item like below.
I added this image to Assets.xcassets and when i tried to set image, it changes color automatically.
how can I set image as it is in bar button item?
Well, you have to edit your image first. Make it transparent (PNG) if it's not, and objects must be be white within the image, in your case it's black now. Then in your code change the tint color like this:
let myImage = UIImage(named: "myImage")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myImage.tintColor = UIColor.blackColor()
I think you need to change the rendering mode of image to always original.
It is taking template image of your image.
let img:UIImage = UIImage(named: "Bitcoin")!
img.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
try changing the rendering mode of the image before setting it to bar button.
for more info on Image Rendering Mode refer this
for Objective C....
UIImage *img = [UIImage imageNamed:#"Bitcoin"];
UIImage *original = [img imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
:)
Try to change the Tint colour.
UIImage* image3 = [UIImage imageNamed:#"search_button.png"];
CGRect frameimg = CGRectMake(15,5, 25,25);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(Search_btn:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarButtonItem =mailbutton;

UIbarbutton blue instead of background image

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

How to make Uibutton selected

I have a UIButton which remains selected when I select that button. Here is my code:
- (IBAction)cloudclick:(UIButton *)sender {
UIImage *bgImage = [UIImage imageNamed:#"black_cloud.png"];
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:sender.tag];
[tmpButton setBackgroundImage:bgImage forState:UIControlStateSelected];
}
But it does not remain selected when I click the button. It changes the background but it does not remain as selected button.
you have to write one line after your code
- (IBAction)cloudclick:(UIButton *)sender {
...
[tmpButton setSelected:YES];
}
use this line of code
sender.selected=!sender.selected;
and set different image for diff states at the time alloc if you are making button from code otherwise set images from xib's.
The various states: UIControlStateNormal, UIControlStateSelected, and (UIControlStateSelected | UIControlStateHighlighted) are all actually distinct. If you want your shadowImage to apply both in the (only) highlighted state and in the highlighted+selected state, you must also set:
[button setBackgroundImage: bgImage forState:(UIControlStateHighlighted | UIControlStateSelected)]
in Swift 5
sender.isSelected.toggle()

UIButton apperance change disclosure too why? how to avoid it?

So when I customize my button I put a background image like this:
- (void) styleUIButtons {
UIImage *buttonNormalBg = [[UIImage imageNamed:#"button_normal" ] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
UIImage *buttonSelectedBgb = [[UIImage imageNamed:#"button_selected" ] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
id appereance = [UIButton appearance];
[appereance setTintColor:self.mainNavigationBarTextColor];
[appereance setBackgroundImage:buttonNormalBg forState:UIControlStateNormal];
[appereance setBackgroundImage:buttonSelectedBgb forState:UIControlStateHighlighted];
}
But the disclosure indicator of the table change horribly to a single line. Why does this happened? and how can I avoid it? Am I customizing the button wrongly?
stretchableImageWithLeftCapWidth:topCapHeight: is deprecated.
Use resizableImageWithCapInsets: instead.
I copied your code and the images are working fine for me.
The only thing I had to add was a frame.
The tintColor only seems to work when there aren't background images.

UIButton set image for state normal, hide that image in different states

[button setImage: [UIImage imageNamed:#"Back.png"] forState:UIControlStateNormal];
[button setTitle: #"Title" forState:UIControlStateSelected];
I would like the button to display the image in normal state but the word "Title" in selected state. But I can't get this code to work. It displays Image when I click the button to selected state and the Image covers the title. I can't get rid of the image in selected state.
[button setImage: [UIImage imageNamed:#"Back.png"] forState:UIControlStateNormal];
[button setImage: [UIImage imageNamed:#"Back2.png"] forState:UIControlStateSelected];
But this works. The image flips from Back to Back2. Or the other way works too
[button setImage: [UIImage imageNamed:#"Back.png"] forState:UIControlStateSelected];
[button setTitle: #"Title" forState:UIControlStateNormal];
It is so simple and I started to think it is UIButton bug.
Has anyone tried this before?
Add some small transparent image (1px is enough) to your project and name it, for example, transparent.png. Then add this line to the code:
[button setImage: [UIImage imageNamed:#"transparent"] forState:UIControlStateSelected];
The other and much simpler solution is to use interface builder and set images for diferent states. In this case you even don't need to use the transparent image; simply left the Image field empty for Selected state.
Like #codeplasma answer, but no need to add a transparent image, just create an empty UIImage:
button.setImage(UIImage(), forState: .selected)
In Objective-C you would create it with [UIImage new].
You have to explicitly set the button state to selected in some UIButtons(like the rounded rect button), it does not happen automatically upon clicking on it.
Bug?
No, it's not a bug, UIButton is designed this way, and here's why: when you set an image for the UIButton's UIControlStateNormal (normal state), it will preserve it throughout the other states (selected, disabled, highlighted) in the event you did not specify an image for those other states.
This means you can set up your button with a single image, the system adds a dark highlight in highlight state and makes the button look like it's alpha 0.5 in disabled state.
You might think setting the image to nil for the selected state will get rid of it for that state, but of course when the state changes, it will see that it doesn't have an image for that state (it's nil !), but it does for state UIControlStateNormal, so it will use that one instead, thus you are stuck with the same image!
Note that if you set the image for a different state then normal, it would have only shown for that state.
Workaround
Honestly your intentions with the button's image/title sound a little odd... Maybe you're new to iOS, maybe with some more info about what your button is and is supposed to be doing, we can suggest a better solution.... but the easiest way to workaround this with the information you've given us is to give a valid image for the selected state, one which is preferably transparent, and it could be 1x1 pixels also to save on space.
Otherwise you can do this, create a UIImage at runtime to use as a placeholder acting as your image, but would be a good idea to cache this, perhaps create it once and hold it in an iVar.
// Create a 1x1 UIImage at runtime
UIGraphicsBeginImageContext((CGSize){1.0f,1.0f});
UIImage *runtimeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Setup your button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20.0, 20.0, 100.0, 100.0);
// set the image for each state
[btn setImage:[UIImage imageNamed:#"Back.png"] forState:UIControlStateNormal];
[btn setImage:runtimeImage forState:UIControlStateSelected];
// have your text for the normal state
[btn setTitle:#"selected !" forState:UIControlStateNormal];
Instead of using setImage, I suggest to use SetBackgroundImage for this scenario. Here is a solution that works for me for a similar solution:
UIImage *cardBackImage = [UIImage imageNamed:#"card.png"];
UIImage *cardFrontImage = [UIImage imageNamed:#"transparent.png"];
[cardButton setBackgroundImage:cardFrontImage forState:UIControlStateSelected];
[cardButton setBackgroundImage:cardFrontImage forState:UIControlStateSelected|UIControlStateDisabled];
[cardButton setBackgroundImage:cardBackImage forState:UIControlStateNormal];

Resources