iOS Button background image not working. (UIButton backgroundImage) [duplicate] - ios

This question already has an answer here:
UIButton image behavior changed in iOS 15?
(1 answer)
Closed last month.
(A) Following line change the image in my imageview.
[(UIImageView *)[cell viewWithTag:400] setImage:[UIImage imageNamed:#"sellsign"]];
(B) Following code change only Title of button. [ I have checked for both UIButton type "System" and "Custom" background image line not working.]
[(UIButton *)[cell viewWithTag:500] setBackgroundImage:[UIImage imageNamed:#"sellsign"] forState:UIControlStateNormal];
[(UIButton *)[cell viewWithTag:500] setTitle:#"1" forState:UIControlStateNormal];
(C) Button Configuration
Button tittle gets change to 1 but there is no change in background image.
Fact 1: I am referring correct button as button title change to 1
Fact 2: Button state is also normal as I can see button title 1
Fact 3: I am referring correct image name as it set in Imageview
Any idea that why only UIButton Background image has this strange behaviour. I think I am missing something in latest iOS.
How can I set my desire image in UIButton as a background image?
Note:
Some one close the question by saying it's duplicate and provided following link,
UIButton image behavior changed in iOS 15?
This is not the solution. It says change the button style. It's patch to achieve output. On that link it's mention that it's old way and one need to adopt new way. Apple introduce new way so there must be solution for it as well.

You need to set the Background Configuration for Background field as Custom also.

Related

Displaying image in UIButton

To preface this question, I am extremely new to Objective-C and Xcode. I have only been learning for a week or so now but I have been stuck on this problem for a few days. I am trying to display an image in a UIButton. I have 3 to choose from, blue.png, purple.png and orange.png. No matter what I do or what image I tell Xcode to use, it will only display the blue image. I have deleted the image from the project entirely. I have "reset content and settings" in the iOS simulator. What exactly is going on here?
the.h code
IBOutlet UIButton *h1;
the.m code
UIImage *buttonImage = [UIImage imageNamed:#"purple.png"];
[h1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:h1];
No matter which image I specify within the quotes, it always uses blue.png, even when the image no longer exists in the project. Thanks for the help everybody!
You must have set blue.png as the background image in the storyboard or xib file. Write NSLog(#"%#",buttonImage) to make sure that the image is not nil. Make sure that you are entering the correct name and extension purple.png
And also, use
[h1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
instead of setImage
I think first of all you should know the difference button.image and button.backgroundImage.
Using the Image (currentImage) field, you can specify an image to
appear within the content of your button. If the button has a title,
this image appears to the left of it, and centered otherwise.
The Background (currentBackgroundImage) field allows you to specify an
image to appear behind button content and fill the entire frame of the
button.
So you can use setImage or setBackgroundImage as what you want.

UIButton Highlighting Overcome default behaviour

In a program much like the who want to be a millionaire... I have 4 option buttons. It should flash red or green on press. The four buttons have either red or green images as background images. In the code setup I have such a method ready to setup the first question and when the second question gets set, the background images change accordingly.
Currently the default blue highlight is always appearing on the first pressing of any one the buttons and after that there are other issues.
I could paste some different approaches here, (but they dont work). So any suggestions would be good.
Also in the IB, when you set the highlight with background image, that works. I would use use, but for the same button, the back ground image must be changed programmatically.
Thanks in advance for the help/suggestions!
Use two State UIControlStateNormal and UIControlStateHighlighted
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1-Highlighted.png"] forState:UIControlStateHighlighted];

uiappearance for UIButton also changes UIBarButton image?

I am using this piece of code
[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
To change the appearance of UIButtons. This works nicely and does not affect UIBarButtonItems except for one of them (The only one that is not an auto generated "back" button) and only initially. When you touch the barbutton, it reverts back to not having a background image.
The problematic button is a UIBarButton and not an UIButton (Unless it is both, in which case the other UIBarButtons should be affected too).
A clue can be that the other UIBarButton that are not affected are all generated and not created by me in the storyboard.
This must be a bug in the api?
Solved using setTintColor. Was an api bug that since then probably has been fixed

How to set the Button text programmatically in iOS?

I know how to change the button text programmatically in iOS:
[myButton setTitle:#"myTitle" forState:UIControlStateNormal];
but my need is I have a label called _lblgenisis, I put some code to change the text of this label and it works fine for me, the code for that is:
_lblGenisis.text = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
my requirement is I have to convert to this code to show the same in button instead of label my button name is _btnGenisis. How to do this?
Solution
Answer that works for me:
NSString *myTitle = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
[nextButton setTitle:myTitle forState:UIControlStateNormal];
Try to add the label to the button?
[_btnGenisis addSubview:_lblGenisis]
Let me see if I get this right.
The code that normally works for changing your button label is not working to set it programmatically?
I have had this issue it turns out that:
When you set the label in the xib file you can see the results and won't make any mistakes. Recently I had a custom button with an image and couldn't figure out why I could not add a title programmatically.
It turns out I was setting the title properly (the way you already know how to). The real issue was that I set my button's image with setImage:forState. This image is above the button and you should check your xib file to see if you have an image set for the button. If so, delete the image and set it as the background image.
To see if this is actually the issue, try this in your code where you are trying to set the title
NSLog(#"image for button %#", _btnGenisis.currentImage);
It should output (null)
If it outputs like you have an image covering your button's title.
Check the interface file and inspect the button. Make sure there is no default image set. You can set a default background image.
If it is all created in code do a search for
[_btnGenisis setImage:
You should not be setting the image if you want a title. Use
[_btnGenesis setBackgroundImage:yourTitleHere forState:UIControlStateNormal];

Round UIButton

I want to know whether drawing a round UIButton(not rounded rect) is possible.
When I add a round image in a UIButton of type custom, then it looks like a round button. But at the moment the button is clicked the boundary of the button becomes visible, so that it looks like a square button, then again when the click ends it looks like a round button.
I want the button to look like a round button even at the moment the click happens. is this possible?
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Result
Just create two rounded button images, one for the pressed state and one for unpressed.
In IB, create a UIButton of type Custom. Go to the section where you set a background image, and set the dropdown with "All" to normal - now set the image to your unpressed image.
Then change the dropdown to "Selected", and put in your pressed image.
Now change the fill type to be aspect fit, and make the button sqare. Use as a normal UIButton anywhere. You can of course also easily do this all programaitcally with similar steps (setting images for UIControlStateNormal and UIControlStateSelected).
You'd probably have to create a custom control for that, but do you really need a round button?
Every platform has its UI conventions, and the iPhone is no exception. Users expect the buttons to be rounded rectangles.
UPDATE in response to comment:
If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.
You can use an UIImageView and its touchesBegan method.
UPDATE 2:
Wait a second. What kind of radio button are we talking about? For some reason I thought you were trying to imitate a real radio. If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker.
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
but.layer.cornerRadius=50;
From what I know of the SDK (admittedly I have very little experience beyond tutorials in books/screencasts), you'd have to create your own control to have a radio button. There are a couple of suggestions on how to implement that in this question.
That said, from a user's perspective I feel like it would be better to stick to the native controls. iPhone users are used to specific controls (i.e. UITableViews or UIPickerViews) for this type of interaction. Straying from practically universal UI conventions tends to make the user experience more jarring.
take the help of this site for creating Round Button on iPhone.
You can get many custom controls :)
http://www.cocoacontrols.com/controls
http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton
H#PPY CODING !
Thanks & regards,
Gautam Tiwari

Resources