UIButton setBackgroundImage and viewWillAppear - uibutton

Ok guys I got a problem with UIButton, maybe that´s simple to solve. I have a button with an IBAction toggleMusic which changes the background image.
-(IBAction)toggleMusic{
if (gameAppDelegate.playMusic) {
gameAppDelegate.playMusic = NO;
[menuAudioPlayer stop];
[botaoMusic setBackgroundImage:[UIImage imageNamed:#"bt_sound_off.png"] forState:UIControlStateNormal];
[SoundManager playSoundFile:#"botao3" Format:#"wav" InLoop:NO];
}
else {
gameAppDelegate.playMusic = YES;
[menuAudioPlayer play];
[botaoMusic setBackgroundImage:[UIImage imageNamed:#"bt_sound_on.png"] forState:UIControlStateNormal];
[SoundManager playSoundFile:#"botao3" Format:#"wav" InLoop:NO];
}
}
This is working fine. playmusic (bool) is a stored variable, so everytime I turn the app off and then on, the app already knows if it should play music or not. this is also working fine.
the problem is that I want to set the correct background image (music on/off) as soon as the app is turned on (not only when I click the button). so I put the following code on view will appear:
-(void)viewWillAppear:(BOOL)animated{
if(gameAppDelegate.playMusic)
[botaoMusic setImage:[UIImage imageNamed:#"bt_sound_on.png"] forState:UIControlStateNormal];
else
[botaoMusic setImage:[UIImage imageNamed:#"bt_sound_off.png"] forState:UIControlStateNormal];
}
when I introduce this code, the app succesfully recognizes the correct background image when the view appears, but togglemusic ceases to alter the button background when I touch the button. It may be something silly, but I can´t figure out what I´m doing wrong...

The one thing I notice is in viewWillAppear you call
[botaoMusic setImage:...];
whereas in the button press you call
[botaoMusic setBackgroundImage:...];
It is likely that it is still functioning, just changing the background image BEHIND the top image of whatever was loaded when the view appears

Related

UIButton with background image: title text randomly does not appear

I am setting the background image on a UIButton which has title text. This works fine, except that about 5% of the time, rarely and non-deterministically, with the same code, the text does not appear.
In my UIButton subclass:
[self setBackgroundImage:normalImage forState:UIControlStateNormal];
// Also set a background image for all other states.
In a UIButton category, I have this convenience method for setting the title:
- (void)setNormalTitle:(NSString *)title {
[self setTitle:title forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateSelected];
[self setTitle:title forState:UIControlStateHighlighted];
[self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted];
}
I'm not finding this issue having been encountered by anyone. Any ideas? I can always create a custom UIView which contains the button and draws text on top using a UILabel, but I wonder if there's a known issue I'm encountering here.
Update: I seem to have worked around the issue by setting the title asynchronously:
_tabCell = loadTopNibObject(#"MYTabSmall3HeaderCell");
dispatch_async(dispatch_get_main_queue(), ^{
_tabCell.tab0Button.normalTitle = #"Option 1";
_tabCell.tab1Button.normalTitle = #"Option 2";
_tabCell.tab2Button.normalTitle = #"Option 3";
});
Update 2: Nope, the above doesn't work. I've breakpointed and printed out the empty button's text, which reports the correct string even though the button appears blank. I've also tried running setNeedsDisplay: when the button is touched, to no avail. Also tried re-setting the button text to some specific string (e.g. #"test") on touch but this doesn't kick in any visible letting.

How do I change a UIButton state programatically?

the .selected and .highlighted properties don't cut it, because for some reason the button looks even more greyed out (darker shade of the non-highlighted image) when highlighted and selected are set to YES.
I need to make my button go off, just as if the user made it go off.
How do I do that?
I now think I understand what you mean. I put a image in my UIButton and tried to change the state of the button on touch down.
- (IBAction)touchDown:(id)sender {
[(UIButton *)sender setHighlighted:FALSE];
[(UIButton *)sender setSelected:FALSE];
}
I noticed that the image does not become darker until you move your finger. If you connect an action to "touch drag inside" and check .highlighted you should see that it has turned TRUE again. You could set it back to FALSE:
- (IBAction)touchMove:(id)sender
{
[(UIButton *)sender setSelected:FALSE];
}
However
If you're only looking for a way to stop the image from turning grey when the user presses it, do this:
button.adjustsImageWhenHighlighted = FALSE;
Setting an image for UIControlStateHighlighted would also remove the greying.
UIImage *image = [UIImage imageNamed:#"img"];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
If by "go off" you mean you want to disable the button, you can use:
[myButton setEnabled:NO];

UIBarButtonItem image (or UIToolbar custom background image?..) is darker (or lighter?..) than original

I want to create custom buttons for UIToolbar (and a custom background color for UIToolbar) in XCode.
So far, I've done it like this: create UIButton, then set an image to it and then drag it to UIToolbar. It becomes UIBarButtonItem after this, but still it has an image so it's fine for me.
The problem is, that for some reason buttons' images became darker than original images (I can see it because my toolbar background is exactly the same as buttons' background), but when I press the button, I see that its image becomes original-like and then after a sec turns back to the darker one.
I've tried to do as said in this question: Image in UIButton is darker tharn original, setting highlighted image in XCode, but after this it stopped 'changing to original' at all.
So, how can I solve this? I don't really want to bring some GUI stuff like this in code if there's a way to do this by XCode... Any help would be appreciated.
EDIT:
That's the screen shot of what's going on:
And that's how they should look like with toolbar background + buttom image combined:
Now I did it in code like this:
#implementation UIBarButtonItem (CustomView)
+ (UIBarButtonItem*) barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[self alloc] initWithCustomView:button];
return item;
}
#end
And the calling code:
UIBarButtonItem *item =[UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"settings"] target:nil action:nil];
[self.bottomBar setItems:#[item]];
For toolbar background I use this code:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"toolbar_background"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
The problem remains the same...
EDIT 2:
Wow, it seems like actually the UIToolbar is lighter than original...
EDIT 3:
Well. Created new project and tested the XCode storyboard-way (with UIButton), there is works fine... That's a mystery for me, totally lost here. Need some serious clarification here about possible reasons of this: my current project is huge and I'm not able to post all the code here...
Well, since nobody answered, I'll just say that I created a new project and the problem gone.
But it happened once again, and I totally don't know why. I did a really lazy and bad thing and changed image brightness for that unlucky button in some image editor =D

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

Adding custom functionality to button

I'm new to iOS and Xcode. I added some buttons to my iPhone app and now I want to add to them functionality. On Xcode I went to the story board file, right click on a button and then there are story board segues there is costume. I'm clicking the + and nothing happen.
[btn addTarget:self action:#selector(youFunction) forControlEvents:UIControlEventTouchUpInside];
[yourButton addTarget:self action:#selector(yourButtonSelected) forControlEvents:UIControlEventTouchUpInside];
Implement -(void)yourButtonSelected method
-(void)yourButtonSelected{
// Write your code
}