How to remove the greyed out look of a disbled UIButton - iphone

I've got a UIButton that i want to look exactly the same when it's in its disabled state as when it's in its Normal state. Right now it has a slight greyed out look to it.

Do not use enabled property or setEnabled:NO method, instead use:
[myButton setUserInteractionEnabled:NO];
That would prevent the button for being touched, but without changing his looks!
The other way is if your button is a custom button and has an image:
[button setImage:someImage forState:UIControlStateNormal];
[button setImage:someImage forState:UIControlStateDisabled];
[button setEnabled:NO];

Related

Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.
I read some posts here, but it's not working.
Is it possible do it in the GUI without programming?
I tried this:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
But what kind of sender events should i associated the function toggleUIButtonImage ?
Thanks
You can do it in interface builder by giving different settings to each button state config:
Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.
[myButton setImage:highlightedImage forState:UIControlStateHighlighted];
Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.
in the ViewDidLoad Method
set the property of
[[button setImage:[UIImage ImageNamed:#"unselectedImage.png"] forState: UIControlStateNormal];
[[button setImage:[UIImage ImageNamed:#"selectedImage.png"] forState: UIControlStateHighlighted ];
This can be done with Interface Builder, no code necessary.
Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field.
Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field.
In IB, you can set the image for the Highlighted (not Selected) state of the button. That should do it.
Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down.

iphone development: one button two actions

In my application I have a button and when it is pressed, lets say "touch Up inside" another view is opened. well my question is, how can I do two actions? I mean, when I pressing the button I want something to do(like hiding the button or changing the image of the button), and when I stop pressing I want to be navigated to the another view.
Do this:
[yourbutton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton addTarget:self action:#selector(touchDown:) forControlEvents:UIControlEventTouchDown];
Now selectors are:
-(IBAction)touchUp :(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:yourImage forState:UIControlStateHighlighted];
}
-(IBAction)touchDown :(id)sender{
//Navigate here
}
If you want to 'do something' when 'pressing', then just hook up 'Touch Down'
The class that is the delegate for the button needs to track the state, and manage that state through the events that can occur (a state machine). Use instance variables to track the state.
When you handle the button action, the code should determine what state it is in, and make any changes necessary to move to the next state.
You can use different actions for different ControlEvents
[button addTarget:self action:#selector(action1) forControlEvents:UIControlEventTouchDown] ;
[button addTarget:self action:#selector(action2) forControlEvents:UIControlEventTouchUpInside] ;
Just add in your code to IBActions. Then like the actions through the interface builder to different touch events.
Code:
[yourUIButton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventXXXXX];
There are a lot of events you can link your UIButton to:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
Ref:UIControl Apple Reference

iphone: unable to change background of buttons

I have a situation I'm trying to change background image of 4 buttons like this:
if(some condition){
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
[secondSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[thirdSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[fourthSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateNormal];
[fifthSeverityButton setBackgroundImage:[UIImage imageNamed:#"redSeverity.png"] forState:UIControlStateNormal];
}
But the background of the other four disappears when I touch any one
Please enlighten me on this how can his be tackled.
May be you are looking for the radiobutton, You can write down a custom class for radio button and make things work out easily.
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
This code actually works fine, with every control state. I think the problem is with the initial image for the button and the current button state (in both nib and code).
The state of buttons can be changed with:
button.enabled=yes;
button.highlighted = NO;
button.selected = NO;
When you setup image for control state specifically, the change on image appear on that specific control state. So the change depends on both the initial image and the current control state of button
NOTE: We are applying change on the background image. There is an image property for the button which can actually hide the changes of background image. If you setup the image
[firstSeverityButton setImage:[UIImage imageNamed:#"greySeverity.jpg"] forState:UIControlStateDisabled];
anywhere in code and maybe it can hide your background image setting.
Also there is a possibility of error in your if-else loop ...go through the loops and verify every conditions whether the images get set properly.

How is a UIButton's image set programmatically?

How does one set the image of a UIButton from within the code?
I try the following but it just leaves the button as a white square.
[A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:#"m1.10001.png"]] forState:UIControlStateNormal];
Your image construction seems a bit overly complicated, but it should work. Don't forget to use the correct button type (custom):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"m1.10001.png"] forState:UIControlStateNormal];
With newer SDKs you can omit the .png. Make sure that the image actually exists (you can store it in a temporary variable and check for nil, for example).
And of course make sure that the button exists (or is bound), i.e. non-nil, when setting the image.

UIImageView as a link to a URL

How to make a UIImageView open a URL in Safari when user clicks it?
I would suggest using a button for this purpose instead. You can create a custom UIButton with whatever image you want. This gives the advantage of providing the built-in target-action mechanism of a button, as well as being able to provided a highlighted image to provide the user feedback. Consider using something like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"regular_image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"highlighted_image.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(loadURL) forControlEvents:UIControlEventTouchUpInside];
Note that it will still look like "just an image" to the user.