Changing Image of Button When the button is selected In Iphone - ios

I have two buttons on my first view,so when I click on one of the button the view changes,so I have two images one for the default state and one for the selected state,
first i tried with xib,goin into the properties and changing the states and then selecting the proper images and when I build and run my code,On cliking the image doesnt change..
So I did this way through code
- (IBAction) handleButton:(id)sender
{
UIButton *button = (UIButton*)sender;
int tag = [button tag];
switch (tag)
{
case BUTTON_1:
if ([m_Button1 isSelected])
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
}
else
{
[m_Button1 setImage:[UIImage imageNamed:#"image_pressed.png"] forState:UIControlStateSelected];
[m_Button1 setSelected:YES];
}
[self displaymethod1];
break;
case BUTTON_2:
[self displaymethod2];
break;
default:
break;
}
}
here the image changes when i click on it and i go to diffrent view..when i again come back to my first view,the button is still in selected mode..so How shall i fix this..
Waiting for your reply

I think is a bit simpler through IB.
When you add a regular Round Rect Button in IB you can modify its behavior by going to the Button section in the Attributes Inspector panel.
First select the images you want for it's default state by leaving the State Config in Default. There is an image and a background image properties for this. Once that is set, you can change the State Config to Highlighted and select the image you want to show when the button is highlighted.
NOTE: This is for xcode4.

when your view will appear then you have to initiate all the variable and UI property
in view will appear method. (not necessary in all cases solution is only for your requirement)
There you can set your button default state and image.
-(void)viewWillAppear:(BOOL)animated
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
...
}

Related

Change image on button click in ios

I have four buttons on a view controller..ViewControllerA.On click of ecah button a viewcontroller in container view opens.I want when button A is clicked then the image on button A changes and image on button B,C,D remains same .when button B is clicked then the image on button B changes and image on buttonA,C,D remains same.
when button C is clicked then the image on button C changes and image on buttonA,B,D remains same.when button D is clicked then the image on button A changes and image on buttonA,B,C. remains same
- (IBAction)AClick:(id)sender {
[_A_btn setImage:[UIImage imageNamed:#“Ahover.png"] forState:UIControlStateNormal];
[_B_btn setImage:[UIImage imageNamed:#“B.png"] forState:UIControlStateHighlighted];
[_C_btn setImage:[UIImage imageNamed:#“C.png"] forState:UIControlStateSelected];
[_D_btn setImage:[UIImage imageNamed:#“D.png"] forState:UIControlStateSelected];
__A_btn.showsTouchWhenHighlighted = YES;
}
I ma following this code .kindly suggest the changes required as it is not working as desired.
first set images for buttons for required states as well, and u can change state of the button on click as follows , it will show the image of that state.
- (IBAction)AClick:(UIButton *)sender {
sender.selected = !sender.selected;
}

UIButton selected text not showing for selected state

All,
I have a UI element that contains a button. I set the text on the button, and simultaneously, the selected state. See below:
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
this is working properly when filled out in the view controller.
However, when I passed the text in from the previous view controller, the selected state text doesn't display. The cancelButton selected state works properly, and the inputButton state is supposedly selected, however, selected state text isn't displaying.
I don't know if it's because the button isn't redrawing or something? I've tried all the [self layoutIfNeed] and [self setNeedsDisplay] with no luck. Anyone know what's going on?
I figured out the issue. I was using both the selected and highlighted states. For some funky reason, that was creating a situation where the button state could be 5. I resolved the problem by not addressing the UIControlStateHighlighted in any way.
Can you check adding following line of code.
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
//Start:Added line of Code
[self.inputButton setTitle:text forState:UIControlStateNormal];
//End:
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
Thanks & Regards,
Amit

UIButton image goes grey on press

I've gone through all the other questions concerning button color changes. Here's the situation, I have a button that when pressed causes a view to slide out. Before press the button's image is white, once pressed goes grey (this is acceptable), but when pressed again to return to original location, the image is still grey. I want it back to white and have tried using UIControlStateNormal, Disabled, etc with no success.
[self.button setImage:[UIImage imageNamed:#"someImage"] forState:UIControlStateNormal];
this has been changed to all UIControl types. currently it is set as:
[self.menu setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[self.menu setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateSelected | UIControlStateHighlighted];
and still no luck. Any suggestions?
You can disable adjusting colours of button's subviews by setting property
myButton.adjustsImageWhenHighlighted = false
you have to set the same image for both states of the button : -
self.button.setImage(ImageName, for: .normal)
self.button.setImage(ImageName, for: .highlighted)
The code you have shown simply declares which image the button will be depending on different states, however you will need to physically change the state of the button too so these images can be used for each state.
What I mean by this, is if you have the button show a different image for when the button is selected, you will need to change the button state to selected. To return the image to the original unselected image, you will need change the state back to unselected. For example, let's say within your viewDidLoad method you have the following code to declare the images for each of the button states:
//Set for normal state
[self.button setImage:[UIImage imageNamed:#"normalImage.png"] forState:UIControlStateNormal];
//Set for selected state
[self.button setImage:[UIImage imageNamed:#"selctedImage.png"] forState:UIControlStateSelected];
Now within your IBAction method you can toggle between the states
-(IBAction*)yourButtonIsPressed:(id)sender{
if (!self.button.selected){ //This is checking button state
//The code will run in here if the button is in a normal, unselected state. This is where you have you method here to slide in view etc
//Now change the button to a selected state
self.button.selected = YES;
}else{
//The code will now run in here if the button is already in a selected state and this is where you place your method to return the view etc
//Now set the button back to an unselected state
self.button.selected = NO;
}
}
I hope this helps
It was something completely overlooked. I had the view opacity set. so:
self.layer.opacity = .60;
was the issue. Once I commented that line out, the button works like a charm. Thanks for the help Jim.
If you created the button programmatically make sure to set the type and change the image render mode to use original to maintain the image color.
let image = UIImage(named: "someImage").withRenderingMode(.alwaysOrigianl)
let button = UIButton(type: .system)

UIButton background image change from another button

Is it possible that I can change the background image of some button when I click on another button?
I basically have 10 buttons, and I want the user to know which button is currently clicked. So what happens is each button has 2 images. One for selected and one for not selected.
I want to create a function which will reset the background images of all the buttons. And in the method for each button, I will add this reset function and then change the background image of that specific button.
Can someone suggest how is that possible?
I know how to change the background image of a button when that button is clicked.
This how my buttons look:
- (IBAction)posterButton:(id)sender {}
- (IBAction)colorInvertButton:(id)sender {}
etc..
Look up the documentation for UIButton: configure each button like this:
[button setControlImage:selectedImage forState:UIControlStateSelected];
[button setControlImage:unselectedImage forState:UIControlStateNormal];
^^this can also be done in interface builder btw.
There are also the states UIControlStateNormal | UIControlStateHighlighted and UIControlStateSelected | UIControlStateHighlighted, but this is optional.
A button also has a selected state, inherited from UIControl.
If you want to have only one selected button at a time, try this (_selectedButton should be declared as an instance variable):
- (UIButton *)selectedButton {
return _selectedButton;
}
- (void)setSelectedButton:(UIButton *)b {
if(b != _selectedButton) {
_selectedButton.selected = NO;
b.selected = YES;
_selectedButton = b;
}
}

UIButton and selected highlight state

The UIButton has a normal/default, highlighted, and selected image. I then have a IBAction method that is called on Touch Down. The method changes the highlighted image depending if it's selected. But when the button is selected, the method is called and so the highlight image is changed, however what is displayed is the normal/default image with a tint. I have tested that image used is in not nil. What happens is when the UIButton in a selected state is pressed displays the normal state with a tint. Why is it not using the highlight image and is there another way of showing a selected highlight image?
Why do you set the highlighted state in the IBAction method? You only need to set the highlighted image for you button when you create it. It will switch automatically. Adding a tint when selected is the default behavior of 'selection' when no highlighted image is assigned.
if your using Interface Builder, just assign the highlighted image there.
Im assuming your looking for normal button selection behavior with your IBAction method set to the touchUpInside event.
I got around this problem by using the selected state and notifications (not via the UI). When a notification is called I change image for the default state, change the selected state and then change the image for selected state.
Update:
I came up with a much better idea from another question that was doing something similar to me. The way to do it is to things in setSelected
- (void)setSelected:(BOOL)selected
{
if ( selected )
{
[self setImage:[CFCHStyleSheet imageForTickButtonChecked] forState:UIControlStateNormal];
[self setImage:[CFCHStyleSheet imageForTickButtonChecked] forState:UIControlStateSelected];
[self setImage:[CFCHStyleSheet imageForTickButtonCheckedDisabled] forState:UIControlStateDisabled];
}
else
{
[self setImage:[CFCHStyleSheet imageForTickButtonUnchecked] forState:UIControlStateNormal];
[self setImage:[CFCHStyleSheet imageForTickButtonUnchecked] forState:UIControlStateSelected];
[self setImage:[CFCHStyleSheet imageForTickButtonUncheckedDisabled] forState:UIControlStateDisabled];
}
[super setSelected:selected];
}

Resources