I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected
Related
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
Hi, I try to set a UIImage in the normal state of UIButton, but when set an UImage in normal state this change the title of UIButton in the selected state changing the title for the image . I am using
myButton setTitle:content forState:UIControlStateSelected
in the state selected.
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
PDT: sorry for my english!.
Try This:
BOOL selected;
-(IBAction)btnClicked:(UIButton *)sender{
if(selected)
{
[chkBtn setTitle:#"selected" forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#" "];
NSLog(#"%#",chkBtn.titleLabel);
selected=NO;
}
else
{
[chkBtn setTitle:#" " forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#"button_minus_pink.png"];
selected=YES;
}
}
// set with UIColor
[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
// set with UIImage
[self.button setBackgroundImage:[UIImage imageNamed:#"BlackRightBtn.png"] forState:UIControlStateNormal];
For Button Selected
if (self.button.isSelected == YES)
{
[self.button setTitle:#"Your_ButtonTitle" forState:UIControlStateNormal];
}
add your code like this:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
where img.png should not be there in your bundle
you can set this through Interface Builder as well.
You can define button and setImage for normal and selected state. Like this way.
buttonJobs = [UIButton buttonWithType:UIButtonTypeCustom ];
buttonJobs.tag = 1;
[buttonJobs setTitle:#"Jobs" forState:UIControlStateNormal];
buttonJobs.frame = CGRectMake(2, 56, 109, 33);
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_Off.png"] forState:UIControlStateNormal];//buttonJobs.png
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_On.png"] forState:UIControlStateDisabled];
[buttonJobs addTarget:self action:#selector(btnNextTab:) forControlEvents:UIControlEventTouchUpInside];
And in button selector method you can set your button selected according sender's tag.
-(void)btnNextTab:(UIButton*)sender
{
if(sender.tag == 1)
{
sender.selected = YES;
}
}
And for Title you can change color of text.
Hope this will help you.
Check with this code:
[myButton setTitle:content forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:yourImage forState:UIControlStateNormal];
Try this code:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
I have created an image button (a.png) infront of each row in the cellForRow method. In the same method itself I'm calling a buttonPressed method which has the code to be executed when the button is pressed. And I want that on the pressing of the button, the button image should be changed to another image (b.png) of the same size.
Please help with how to do that.
[but setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
try this......
Create Custom Cell for UITableView and UIButton on Custom Cell.
Create property for UIButtion.
In cellForRowAtIndexPath: method
//Set Action on Button in Table View Row
[cell.UIButtonOutlet addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
This will create action from your button.
And in method
-(void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches=[event allTouches];
UITouch *touch=[touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];
//GET IndexPath
NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];
selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];
[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Try the following method
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(12,12,200,200)];
[button addTarget:self action:#selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
then in the click action method of button
-(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
[self yourFunction:(UIButton *)button];
}
then finally
-(void)yourFunction:(UIButton *)button{
button.selected = ! button.selected;
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
}
}
Try this when initializing your button
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Then modify your method(which is called when button is pressed) like this
-(void)yourMethod:(id)sender{
UIButton *btn=(UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
//your other code
}
In my app am using UIScrollView. I added buttons to UIScrollView.
For each button i assigned image using
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
and actions.
forState i used UIControlStateNormal in viewDidLoad.
And when we click on button image has to change.
For this i used same method but forState value changed to UIControlStateSelected in ViewDidLoad.
But button image is not changing.
any one can help or suggest
You've to use like this
In ViewDidLoad Set images for normal and selected state
-(void)ViewDidLaod
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateSelected];
To perform selection image change can be done below code
-(void)buttonclick
[myButton setSelected:YES];
or
[myButton setSelected:NO];
Use this line of code I think this will be helpful for you.
-(void)viewDidLoad
{
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
}
-(void)compete
{
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateNormal];
}
use
[button setImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"button_highlighed.png"] forState:UIControlStateHighlighted];
I'm trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS.
The code I'm using is
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
That works fine for now.
But my problem is, that when I'm toggling on, I press it on, then it is popping off again and then finally on.
The app works, but that is really ugly, though.
I firstly set the "highlighted" image to on. So when I highlight the button, it is on and that popping to on. That works fine. But when I turn it off again, the problem is the same, in the reverse direction.
I tried to put that code:
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage[UIImage imageNamed#"off.png"] forState:UIControlStateHighlighted];
}
else
{
[button setImage[UIImage imageNamed#"on.png"] forState:UIControlStateHighlighted];
}
button.selected = !button.selected;
}
But as long button.selected = !button.selected there is no difference.
So it won't make any change.
I also tried to trigger the IBAction on »Touch Down« but you can imagine how frustrating this will look like.
Has anybody got a solution for that problem?
Did anybody struggle with that one too?
Greets, thanks a lot
Julian
Don't manually switch the images around, just set the selected state's image in Interface Builder and swap the selected property over when the button is tapped.
I've had a similar problem to this before, the button works a little strangely when tapping. Try this code and let me know if it works
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
}
button.selected = !button.selected;
If (button.selected) {
[button setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateNormal];
} else {
[button setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateNormal];
}
When you tap and hold on a button the state is actually Highlighted & Selected so you need an image for both Highlight and selected state.
Agree with Jim, your code needs little modification as below...
[button setImage[UIImage imageNamed#"off.png"] forState:UIControlStateNormal];
[button setImage[UIImage imageNamed#"on.png"] forState:UIControlStateSelected];
Have two buttons created.
place buttons one above one.
Can set Default & Selected images using Custom Button option in design builder.
-(void)firstButtonClicked {
firstButton.hidden = YES;
secondButton.hidden = NO;
}
-(void)secondButtonClicked {
firstButton.hidden = NO;
secondButton.hidden = YES;
}
#Jim - when do u switch the isSelected state? isHighlighted gets called twice for every press. My hacked version of UIButton switch below : -
class ButtonSwitch: UIButton {
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
if allControlEvents == .touchUpInside {
isSelected.toggle()
}
super.sendAction(action, to: target, for: event)
}
}
I have a UIButton which I assign an image in IB.
I then change the image in the code, but I only want this to be temporary.
How does one return the button to the image set in IB or is there a way to revert back after doing the temporary switch?
Thanks
If you want to revert back to the previous image you would have to store a reference to it somewhere:
// prevImage is an ivar UIImage *
prevImage = [[myButton imageForState:UIControlStateNormal] retain];
// Change to the new image
...
// Later: revert back to prevImage
[myButton setImage:prevImage forState:UIControlStateNormal];
[prevImage release];
prevImage = nil;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateDisabled];
You can set your own custom images for different states. when you tap on it, the corresponding images will appear on your button.