iOS: change png in a bar button item - ios

I put in a tolbar a bar button item, and I set it with a png (first.png) and when I push it I want to change its png in "second.png"
This code doesn't work fine:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
if ([sender isSelected])
{
[sender setImage:first forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
[sender setImage:second forState:UIControlStateSelected];
[sender setSelected:YES];
}

The current answerers do not realize that UIBarButtonItem does NOT inherit from UIButton, so setImage:forState: will most definitely not work. UIBarButtonItems cannot be set for different states. You can, however, utilize something like this (declared in the UIBarItem docs):
sender.image = [UIImage imageNamed:#"first.png"];

I'm not clear on why you have an if block in there. Can't you initialize both state images as a one-off initialization step? I.e.
[sender setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
In fact, this can probably be done in Interface Builder if you don't need to do it programmatically.

Just write after creation of button this lines:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
[sender setImage:first forState:UIControlStateNormal];
[sender setImage:second forState:UIControlStateSelected];
You don't need if-else statement.
Updated:
CGRect myFrame;
UIButton *myButton = [[UIButton alloc] initWithFrame:myFrame];
[myButton setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[myButton release];

Related

Previous Radio buttons state not cleared objective c

What I want?
Custom radio buttons, allowing a user to select a single option among multiple options
Code
- (void)setupBtn{
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,18,18)];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:#selector(radioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view2 addSubview:self.radioBtn];
}
- (IBAction)radioButtonTapped:(UIButton*)sender{
if (self.radioBtn.isSelected == true) {
[self.radioBtn setSelected:NO];
self.view1.backgroundColor =[UIColor clearColor];
[self.radioBtn setImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}else {
[self.radioBtn setSelected:YES];
self.view1.backgroundColor = [UACFCustomColors continentalLightBlueTransparent];
[self.radioBtn setImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateSelected];
}
}
Try storing all your buttons in an array. When one gets tapped, loop through the array, and check if each button is selected. If the button is selected, set it to not be, then exit the loop. After the loop, set the button that was tapped (sender) to be selected.
Untested example code:
- (void) viewDidLoad {
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn setSelected:YES];
[self.view2 addSubview:self.radioBtn];
self.radioBtn2 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn2 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn2 setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn2 setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn2 addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn2 setSelected:NO];
[self.view2 addSubview:self.radioBtn2];
self.radioBtn3 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn3 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn3 setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn3 setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn3 addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn3 setSelected:NO];
[self.view2 addSubview:self.radioBtn3];
self.buttonArray = #[self.radioBtn, self.radioBtn2, self.radioBtn3];
}
- (void) myButtonTapped:(UIButton*) sender {
for(UIButton *btn in self.buttonArray) {
// Only one button should be selected at a time, so once we find it,
// set it to not be selected then break from the loop
if(btn.isSelected){
[btn setSelected:NO];
break;
}
}
// set the button that was tapped to be selected
[sender setSelected:YES];
}
To send one button's text to another button's is fairly simple.
[btn2 setTitle:[btn1 titleForState:UIControlStateNormal] forState:UIControlStateNormal];

UIButton is nil after init

I am trying to create a UIButton - this works fine when I am using the simulator, but when I try on a device the button does not appear.
[self.followButton setImage:[UIImage new] forState:UIControlStateNormal];
[self.followButton setTitle:#"EDIT" forState:UIControlStateNormal];
self.profMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *icon = [[UIImage imageNamed:#"More"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.profMoreButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.profMoreButton setImage:icon forState:UIControlStateNormal];
[self.profMoreButton.imageView setTintColor:[UIColor Primary]];
[self.profMoreButton addTarget:self action:#selector(morePressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.profMoreButton];
[self.profMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self.followButton.mas_height).multipliedBy(0.6);
make.centerY.equalTo(self.followButton);
make.left.equalTo(self.followButton.mas_right).with.offset(8);
}];
if (!self.profMoreButton) {
NSLog(#"WHY!?????");
}
On my iPhone 6 the NSLog at the end is always printed and the button doesnt appear UNLESS I put a breakpoint at the top and step my way through.
self.profMoreButton was declared as a weak IBOutlet, I removed the IBOutlet and changed it to strong. Now it works 😳
I THINK YOU DONT GIVEN ALLOC UIBUTTON
settingsButton =[UIButton buttonWithType:UIButtonTypeCustom];
settingsButton.frame= CGRectMake(SCREEN_WIDTH-[UIImage imageNamed:#"setting.png"].size.width-30, 22, [UIImage imageNamed:#"setting.png"].size.width, [UIImage imageNamed:#"setting.png"].size.height);
[settingsButton setImage:[UIImage imageNamed:#"setting.png"] forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingButtonMethod) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setMultipleTouchEnabled:NO];
settingsButton.exclusiveTouch=YES;
settingsButton.backgroundColor=CLEAR_COLOR;
[self.view addSubview:settingsButton];

iOS: How to cover entire button with image

I am currently working on a iOS application where I am constantly changing the pictures of two buttons. The problem is that the picture of the button that constantly changes does not stretch over the entire button. I was wondering what is the best way of doing this.
I believe I am setting my image of my button normally and I cant figure out what to do. Any suggestions will be greatly appreciated.
leftButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [orangeImageArray objectAtIndex:randomPicture2]]] forState:UIControlStateNormal];
Is your button type UIButtonTypeCustom ?
example :
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:btnImage forState:UIControlStateNormal];
Try this:
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[leftButton setImage:btnImage forState:UIControlStateNormal];
This code enables you to customize the button well:
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn addTarget:self action:#selector(prefer) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barButton;

How to switch back to original button image after delay (obj-c)?

Hi I am trying to make a simple app that includes buttons changing images on click. I would like them to switch back to the original after a delay of 2 - 3 seconds. Im new to objective c and cant figure out how to do it. I've tried variations of the code below. Since there are several buttons I'd need to retain the sender id or image name. Thanks in advance!
- (IBAction)playSound:(id)sender {
UIImage *newImage = [UIImage imageNamed:#"new.jpg"];
[sender setImage:newImage forState:UIControlStateNormal];
[NSThread sleepForTimeInterval:3];
UIImage *origImage = [UIImage imageNamed:#"orig.jpg"];
[sender setImage:origImage forState:UIControlStateNormal];
}
Add two target of your Button with StateHighlighted and StateNormal, such like,,,
[myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d-active.png", i]] forState:UIControlStateHighlighted]
[myButton addTarget:self action:#selector(buttonMenuPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:#selector(gotoList:) forControlEvents:UIControlStateHighlighted];
You need not to change button image. Follow this:
Your UIButton will have to images for two states, UIControlStateNormal and UIControlStateSelected in loadView or in viewDidLoad of in xib itself.
[button setImage:[UIImage imageNamed:#"original.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"new.jpg"] forState:UIControlStateSelected];
when a user will click on button, button will turn to [UIImage imageNamed:#"new.jpg"] itself. Then inside playSound method fire a NSTimer
-(IBAction)playSound:(id)sender
{
[self aTimer];
}
- (void)aTimer
{
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(changeImage) userInfo:self.view repeats:NO];
}
- (void)changeImage
{
[button setSelected:NO];
}

Highlight the UIButtons created Dynamically?

In my application I have created 20 buttons with in a scroll view, now problem is that I was not able to highlight the selected button.
My intention is to show the pressed button with a different look than normal. When another button is pressed the previous one should become normal:
UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[Abutton setTag:i-1];
Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
[Abutton setTitle:#"" forState:UIControlStateNormal];
Abutton.backgroundColor = [UIColor clearColor];
[Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"image1.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"image2.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[Abutton addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:Abutton];
Finally I created the method for Abutton pressed as below:
-(IBAction)buttonpressed:(id)sender{
Abutton.highlighted=YES;
//.....
//.....
}
If do it like this then only the last button created dynamically gets highlighted. That's not exactly what I wanted.
I think you should replace your current code for the button pressed:
-(IBAction)buttonpressed:(id)sender{
UIButton *b = (UIButton *)sender;
b.highlighted = YES;
//.....
//.....
}
In your example you are specifically highlighting "AButton". This code highlights the button being pressed.
Solution 1:
Create an NSSet with references to all the buttons. In your buttonPressed method, call makeObjectsPerformSelector on the NSSet's buttons, setting them to the unhighlighted state.
Solution 2:
Use a UISegmentedControl. That seems like what you should be doing in this case anyway.

Resources