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

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.

Related

Can't get object while using UIImage imageNamed:#"cardback"

I'm a beginner at Objective C.
I'm trying to add a button to the storyboard. When clicked, the button's background will be changed to another image. However, I'm try to get the background image using :
[sender setBackgroundImage: [UIImage imageNamed:#"cardback.jpg"] forState:UIControlStateApplication];
It seems that [UIImage imageNamed:#"cardback.jpg"] just doesn't get the image object for me.
codes
You are using wrong control state flag. You need to use UIControlStateNormal instead of UIControlStateApplication for getting the desired result. Refer UIControlState for more info. Also you don't need to specify the image extension (because you are loading the image from asset catalog), so remove .jpg from the code.
Change:
[sender setBackgroundImage: [UIImage imageNamed:#"cardback.jpg"] forState:UIControlStateApplication];
to
[sender setBackgroundImage: [UIImage imageNamed:#"cardback"] forState:UIControlStateNormal];

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;

Blue image for buttons

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]

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

UIButton wierdness: Unable to set title for a custom button type

I'm trying to set the title for a custom button that I've created programmatically. The button's image and the frame come up fine, but the title doesn't. I can't really think of anything wrong with this code, so any help is apreciated!
self.helpButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.helpButton setFrame:CGRectMake(113.0, 685.5, 73.0, 40.0)];
UIImage *helpImg = [UIImage imageNamed:#"11_HelpCancel_Up.png"];
[self.helpButton setImage:helpImg
forState:UIControlStateNormal];
[self.helpButton setTitle:#"Help" forState:UIControlStateNormal];
[self.helpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// [self.helpButton setFont:[UIFont boldSystemFontOfSize:14.0]];
[self.view addSubview:self.helpButton];
Thanks,
Teja.
Use
[self.helpButton setBackgroundImage:helpImg forState:UIControlStateNormal];
- setImage:forState: seems to override - setTitle:forState:
Was having trouble with the button title showing up on my storyboard when I switched the type of button from system -> custom. The problem was the color of the text changed to white when I made the change so anyone having a similar issue, make sure to check the color of the button text.

Resources