How to set an invisible title on UIButton? - ios

I'm working on an Ipad application, and i need to set a title on a 'UIButton', in order to could use :
-(void)MyMethod:(id)sender
{
UIButton *resultButton = (UIButton *)sender;
if ([resultButton.currentTitle isEqualToString:#"TitleName"])
{
...
}
}
But when I use this sort of code :
[MyUibutton setTitle:#"TitleName" forState:UIControlStateNormal];
It appears "TitleName" behind the image of the 'UIButton'...
So, is it possible to fix it ?
Thanks !

Don't use the button title, it's for display, not a flag. Instead use a tag or objc_setAssociatedObject Or store the button in a dictionary and compare.

theButton.titleLabel.alpha = 0;

If you set the image of the button, it will be at the top of the button, so the button's title won't be visible. Set the button's background and it will solve your problem !
[yourbutton setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];

You can do [button setTitle:title forState:UIControlStateDisabled]; as long as you know you are not ever going to disable the button. Other than that, using the tag property is probably the easiest.

Related

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

Changing button image when another button is clicked

I'm facing a problem in xcode, in one of my view controllers i have two buttons, when one of them is clicked (appleBtn) the second button's (topMenu1Btn) image should change
I created an outlet for both buttons, an action for the applebtn when clicked
- (IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView = [UIImage imageNamed:#"Apple.jpg"]
}
the thing is the imageview is read-only
is there anyway to do this?
- (IBAction)HandleBtnClick:(id)sender
{
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
}
You could try this to set the image for the button.
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you want the background image use setBackgroundImage: instead
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
Or
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you use the second method then make sure that the frame of image matches with the button
Make sure that Button type is Custom
(IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView.image = [UIImage imageNamed:#"Apple.jpg"]
}
You can't modify the imageview, but you can modify the image in it ;)

Setting an UIImage for UIControlStateHighlighted

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

How to disable the highlight control state of a UIButton?

I've got a UIButton that, when selected, shouldn't change state when being touched.
The default behaviour is for it to be in UIControlStateHighlighted while being touched, and this is making me angry.
Suggestions?
Your button must have its buttonType set to Custom.
In IB you can uncheck "Highlight adjusts image".
Programmatically you can use theButton.adjustsImageWhenHighlighted = NO;
Similar options are available for the "disabled" state as well.
In addition to above answer of unchecking "highlight adjusts image" in IB, make sure that button type is set CUSTOM.
This will work for you:
[button setBackgroundImage:[UIImage imageNamed:#"button_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"button_image_selected"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"button_image_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];
3rd line is the trick here...
This works the same for setting image/backgroundImage
adjustsImageWhenHighlighted = NO;
button.adjustsImageWhenDisabled = NO;
is equally useful for having your own appearance of a disabled button.
Depending on what changes from the default to the highlighted state of the button, you can call a couple of methods to set them to what you need. So if the image changes you can do
[myButton setImage:[myButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];
If the text changes you can do
[myButton setTitle:[myButton titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];
other similar functions:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
For Swifty Developer -
yourButton.adjustsImageWhenHighlighted = false
Swift 3+
button.adjustsImageWhenHighlighted = false
button.adjustsImageWhenDisabled = false
OK here's an easy solution if this works for you, after a week of banging my head on this it finally occurred to me to just set highlighted=NO for the 1st line of the IBAction method for the TouchUpInside or TouchDown, or whatever works. For me it was fine on the TouchUpInside.
-(IBAction)selfDismiss:(id)sender {
self.btnImage.highlighted = NO;
NSLog(#"selfDismiss");
etc, etc, etc.
}
make your button Type - "Custom"
and Uncheck - Highlighted Adjust image and
you are done.
just two things:
UIButton *btnTransparentComponent = [UIButton buttonWithType:UIButtonTypeCustom];
btnTransparentComponent.adjustsImageWhenHighlighted = NO;
I had a similar issue and found that "unchecking" Clears Graphic Content in interface builder fixed my issue
After the introduction of Style, you have to set the style to Default in IB along with setting the type to Custom to be able to disable the highlighting effect completely. Otherwise your button text will keep highlighting.
*Setting the Style to Default resets the text color to white.
avoid to set UIButton's Line Break to Clip, use instead the standard Truncate Middle

is it possible to update UIButton title/text programmatically?

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I'd like to update the title/text of the UIButton to reflect the new state. I'm calling:
[myButton setTitle: #"myTitle" forState: UIControlStateNormal];
[myButton setTitle: #"myTitle" forState: UIControlStateApplication];
[myButton setTitle: #"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: #"myTitle" forState: UIControlStateReserved];
[myButton setTitle: #"myTitle" forState: UIControlStateSelected];
[myButton setTitle: #"myTitle" forState: UIControlStateDisabled];
But it never seems to change from the original text/title as specified in IB.
I solved the problem just setting the title parameter for UIControlStateNormal, and it automatically works on the other states. The problem seems to be when you set another UIControlState.
[myButton setTitle: #"myTitle" forState: UIControlStateNormal];
Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.
Edit: While it's not the case here, something like this can also happen if you set an attributed title to the button, then you try to change the title and not the attributed title.
As of Swift 4:
button.setTitle("Click", for: .normal)
I discovered another problem. It may be a bug introduced in iOS 5, but I thought I'd point it out for anyone else who encounters it.
If you don't set any default text for the button in the XIB, no text will ever appear if you set it programmatically. And if you do set text in the XIB, any text you subsequently assign to the button programmatically will be truncated to the size of the default text.
And finally, if you're showing the view with your button and then invoke another view (like an ActionSheet) and then dismiss it, the text that you assigned to the button programmatically will be erased and the button caption will return to whatever you set up in the XIB.
Even though Caffeine Coma's issue was resolved, I would like to offer another potential cause for the title not showing up on a UIButton.
If you set an image for the UIButton using
- (void)setImage:(UIImage *)image forState:(UIControlState)state
It can cover the title. I found this out the hard way and imagine some of you end up reading this page for the same reason.
Use this method instead
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
for the button image and the title will not be affected.
I tested this with programmatically created buttons and buttons created in a .xib
for swift :
button.setTitle("Swift", forState: UIControlState.Normal)
One more possible cause is this:
If you attempt to set the button's title in the (id)initWithNibName: ... method, then you're button property will still be nil. It hasn't yet been assigned to the UIButton.
You must be sure that you're setting your buttons in a method like (void)viewWillLoad or (void)viewWillAppear, but you probably don't want to set them as late as (void)viewDidAppear.
Turns out the docs tell you the answer! The UIButton will ignore the title change if it already has an Attributed String to use (with seems to be the default you get when using Xcode interface builder).
I used the following:
[self.loginButton
setAttributedTitle:[[NSAttributedString alloc] initWithString:#"Error !!!" attributes:nil]
forState:UIControlStateDisabled];
[self.loginButton setEnabled:NO];
Sometimes it can get really complicated. The easy way is to "refresh" the button view!
//Do stuff to your button here. For example:
[mybutton setEnabled:YES];
//Refresh it to new state.
[mybutton setNeedsDisplay];
I kept having problems with this, the only solution was to add an image and label as subviews to the uibutton. Then I discovered that the main problem was that I was using a UIButton with title: Attributed. When I changed it to Plain, just setting the titleLabel.text did the trick!
#funroll is absolutely right. Here you can see what you will need Make sure function runs on main thread only. If you do not want deal with threads you can do like this for example: create NSUserDefaults and in ViewDidLoad cheking condition was pressed button in another View or not (in another View set in NSUserDefaults needed information) and depending on the conditions set needed title for your UIButton, so [yourButton setTitle: #"Title" forState: UIControlStateNormal];
Make sure you're on the main thread.
If not, it will still save the button text. It will be there when you inspect the object in the debugger. But it won't actually update the view.

Resources