UIButton Image being distorted in tableviewcell - ios

I have a UIButton in each tableviewcell that I have an image in. Basically I set the image to something different based on another parameter. You can ignore the totalTime/activityTimeInt stuff, but if it helps, they are just ints ( say totaltime = 60, activitytime = 50). The following is the cellforrowatindexpath:
if(totalTime <= activityTimeInt){
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:#"travelDistanceGraphicRed.png"] forState:UIControlStateNormal];
[cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
if(totalTime + 10 >= activityTimeInt){
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:#"travelDistanceGraphicYellow.png"] forState:UIControlStateNormal];
[cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
if(totalTime >= activityTimeInt){
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:#"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
[cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
}
else if (!totalTime){
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:#"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
[cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
return cell;
For some reason when the if statement runs through
else if (!totalTime){
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:#"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
[cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
where totalTime doesn't exist, the images look like:
and when it runs through the other 3 if statements (with totalTime), they look like:
If you need more info just ask. Thanks in advance.

you need to use this code
[cell.vicinitimeGraphicButton setBackgroundImage:[UIImage imageNamed:#"travelDistanceGraphicRed.png"]
forState:UIControlStateNormal];
since
setImage:forState: sets the image as the actual content of the button. For example, you can not see the button title even though you set it, because you have set an image as the content or else you can see both image on left and text on right which is in you case
setBackgroundImage:forState: sets the image as the background. In this case, you can set the title and it is displayed on top of the image.
Note that you are not setting default Background Image to you Button

I figured out that the problem was that I had set the default image in the customtablecell.xib file, when I shouldn't have. fail on my part. however, it should be setbackgroundimage like ayush said above.
the default image shouldn't be set to anything.

Related

Disabled buttons not changing image in

I have a button that pushes to a settings viewController. On that settings viewController I has a switch to invert all the colors on the original view, an inversion which I've done programmatically. I made inverted button images to replace the original images.
When I return to the original view, I have viewWillAppear call the method to invert if the switch has been flipped. Everything changes accordingly except for two disabled buttons.
The switch value is saved under the default settings so that the user can exit the app and come back later and still have the colors inverted. When viewDidLoad calls the invert method, the buttons change just fine, and they even show up adjusted to show they are disabled.
Any idea what might be going on?
-(void)invert{
self.view.backgroundColor = [UIColor blackColor];
//Buttons
[self.stopButton setImage:[UIImage imageNamed:#"stopinverted"]
forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:#"playinverted"]
forState:UIControlStateNormal];
}
-(void)viewWillAppear:(BOOL)animated{
_inverted =
[[NSUserDefaults standardUserDefaults] boolForKey:#"isInvertedOn"];
[[NSUserDefaults standardUserDefaults] synchronize];
if(_inverted){
[self invert];
} else {
[self unInvert];
}
[super viewWillAppear:(BOOL)animated];
}
The issue seems to be that the image is not updated automatically, you'll need to reset the enabled value to make it work:
[self.stopButton setImage:[UIImage imageNamed:#"stopinverted"]
forState:UIControlStateNormal];
self.stopButton.enabled = ! self.stopButton.enabled;
self.stopButton.enabled = ! self.stopButton.enabled;
[self.playButton setImage:[UIImage imageNamed:#"playinverted"]
forState:UIControlStateNormal];
self.playButton.enabled = ! self.playButton.enabled;
self.playButton.enabled = ! self.playButton.enabled;
I think you are disabling stopButton and playButton before applying the image. And also you are applying UIImage for UIButton normal UIControlState. I would suggest do the following code changes
Change this
//Buttons
[self.stopButton setImage:[UIImage imageNamed:#"stopinverted"]
forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:#"playinverted"]
forState:UIControlStateNormal];
to
//Buttons
[self.stopButton setImage:[UIImage imageNamed:#"stopinverted"]
forState:UIControlStateDisabled];
[self.playButton setImage:[UIImage imageNamed:#"playinverted"]
forState:UIControlStateDisabled];
In short you have to set UIButton UIImages in disabled state.
For more reference have a look this questions answer Xcode: set image on button only for default state, not also selected
If you set the UIImages according to the state of the UIButton then in the function in which you want to invert the UIImage, you have to only need to handle the state of the UIButton and it will automatically change the image.
Thanks for the help. Here is the answer I came up with, inspired by A-Live's answer.
First I had to set the button as enabled, then set the image, and then disable the button again all within the same method. This has fixed my problem.
[self.playButton setEnabled:YES];
[self.playButton setImage:[UIImage imageNamed:#"playinverted"]forState:UIControlStateNormal];
[self.playButton setEnabled:NO];
try using :
theButton.adjustsImageWhenDisabled = false
this will use your custom image instead of the default dimming.

UIButton title text is not updated even if I update it in Main thread

I'm trying to change the title of an UIButton I've created programmatically, when the user clicks in it. So, this is my code to create the UIButton:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:#"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:myButton];
And, in my userClicked: method, I do:
-(void) userClicked:(UIButton*)button
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
[button setTitle:#"Bye" forState:UIControlStateNormal];
[button setTitle:#"Bye" forState:UIControlStateSelected];
[self someLengthyComputation];
}
The weird thing is that I can see the log messages printed:
USER CLICKED!!!
isMainThread
But, the title of the button does not change! What am I doing wrong?
EDIT: Setting the title for several states doesn't work either.
EDIT2: If I print the description of button in the debugger window of Xcode, it shows the right title!
Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>
This worked for me to update the title text (iOS 7.1, Xcode 5.1):
button.enabled = FALSE;
[button setTitle:#"Test" forState:UIControlStateNormal];
button.enabled = TRUE;
I was having the same problem, and I noticed that everywhere else I was setting the attributedTitle. So any updates to the title were not affecting the attributed title.
My solution:
[button setAttributedTitle:#"" forState:UIControlStateNormal];
instead of
[button setTitle:#"" forState:UIControlStateNormal];
At the moment, the shortest work around I came with is calling:
[button setNeedsLayout];
After updating the title.
This seems to happen in iOS 7.1.
All my buttons, which were behaving correctly in previous iOS versions (or maybe just compiled with previous SDKs) suddenly stopped doing that when compiled in Xcode 5.1.1 SDK 7.1.
Looks like Apple's bug.
I had a similar problem using storyboards. Using the answers above I had to use
[mybutton setTitle:#"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];
AND I had to make sure that the button type was 'Custom' not 'System' in the attributes inspector.
Please see if this might help you...when the button is clicked check for condition if buttonToggled...like below when you have a function like changeButtonText
-(IBAction)changeButtonText:(id)sender {
if (!buttonToggled) {
[sender setTitle:#"Initial Text" forState:UIControlStateNormal];
buttonToggled = YES;
} else {
[sender setTitle:#"Alternative Text" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
There are several issues in your code:
You are assigning callback to the button:
#selector(userClicked:)
but your code is in another method:
-(void)userTapOnTapToRefreshView:(UIButton*)button
To fix that you need to implement something like this:
-(void)userClicked:(id)sender
{
[(UIButton*)sender setTitle:#"Bye" forState:UIControlStateNormal];
}
Also this part of code does not make sense for me:
[parentView myButton];
Try to change it to:
[parentView addSubview:myButton];
Hope it will help :)
For Swift 3 to 5 just use the following:
button.setTitle("My title", for: .normal)
or for a attributed text use this:
button.setAttributedTitle(<AttributedString>, for: .normal)
This is kinda late and somewhat relates to Walter Schurter's response:
I had a similar issue where my button text was being set correctly until I updated to 7.1. Then I found that since my button was disabled, I had to set the title color and title text for the disabled state to get the text to show up. Then everything worked like a charm!
In iOS 7, UIButton's title is not updated when it is disabled. It seems like a bug on iOS 7.
As a workaround, update both normal and disabled title. It works!
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
Try this:
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
or:
[button setTitle:#"Bye" forState:UIControlStateSelected];
You could also modify your void function:
-(void) userClicked
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[myButton setTitle:#"Bye" forState:UIControlStateNormal];
}
In my case I tried everything but nothing was working.
Then I just changed button type from system to custom from storyboard. BOOM! everything started behaving normally.
Change the button type to 'Custom', instead of 'System' and it will work as expected :)
Well, is all about the enabled state of a UIButton, Apple has changed something in 7.1 that does not allow you to change the title if you have the UIButton on a disabled state, thats all.
Thanks Apple, i have lost 10 min. debuging an app that was working fine.
Just found out this morning, got updated XCode yesterday to the 5.1.1 and iOS to 7.1
It could be the button layout refresh issue.....
Try using...
[button setNeedsLayout];
[button layoutIfNeeded];
It will force button to update the layout.
Finally, I've figured it out. There were two problems:
1) button was not in state UIControlStateNormal.
2) I was calling a method performing a long computation just after setting the title, [self someLengthyComputation].
I've solved the problem by:
1) Setting the title for all states of the button.
2) Performing that big computation in another thread, not the main thread.
My working code now is:
-(void) userClicked:(UIButton*)button
{
NSLog(#"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(#"is main thread");
}
[button setTitle:#"Bye" forState:UIControlStateHighlighted];
[button setTitle:#"Bye" forState:UIControlStateNormal];
[button setTitle:#"Bye" forState:UIControlStateSelected];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self someLengthyComputation];
});
}
Thank you very much to everybody who has answered/commented!
This may be trivial, but if you set some button image (instead of background image) which fills the whole button frame, this will shift the title right and thus make it invisible.
If so, try changing button image to background image.
Per Apple developer guide, you should not set either the button title label text or color directly as a property (for example, do not do this: myButton.titleLabel.text=#"SomeText" or myButton.titleLabel.textColor=[UIColor blackColor]).
Rather, you set them using the UIButton setter functionality, like this:
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
...or like this:
[myButton setTitle:#"SomeText" forState:UIControlStateNormal];.
See the Apple guide.
Conclusion after trying many solutions is to use setAttributedTitle instead of setTitle.
To make the title string for AttributedString:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:#" new Test "];
[button setAttributedTitle:attString forState:UIControlStateNormal];
By the way, this problem is not occasionally happen in normal but it suddenly happen mainly for the following reasons:
If you change the enabled state of a UIButton and try to change the title.
If you use an attributed value and then want to change it using setTitle, the attributed value is superior to the title in that case.
If you navigate to another view controller and then return back to update the button title.
There is no requirement to use:
[button setNeedsLayout];
[button layoutIfNeeded];
Instead, first set type as DEFAULT to CUSTOM
If you applied setAttributedTitle then use:
[button setAttributedTitle:[NSAttributedString new] forState:UIControlStateNormal];
Otherwise there is no need to change anything.
If the color of the text has not changed then apply same thing and set title color with:
[button setTitleColor:[any_color] forState:UIControlStateNormal];
None of the above worked for my case (I was having a button on top of a view presented with UIPopoverController), and I had to removeFromSuperview before setTitle, then addSubview back - hope this helps someone with similar issues
I doubt if 'button' passed in as a parameter is myButton.Anyway,if myButton is a member var,you can just [myButton setTitle:#"Bye" forState:UIControlStateNormal];
Try to declare you button as a property, either in the interface or in the implementation part of you view / view controller
#property(nonatomic,strong) UIButton* myButton;
then create it
self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[_myButton setBackgroundColor:[UIColor blackColor]];
...
It should work. Don't ask me why really - I just think it's all about ARC / Modern memory management..
EDIT:
It should also work if you keep a reference on the button in your implementation..
#implementation myViewController
{
UIButton* myButton;
}
-(void)createButton
{
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
...
}
...
instead of
yourButton.titleLabel?.text = "T"
use this
yourButton.setTitle("TITLE", for: .normal)
Always do addSubview before setting the text on UIButton.

How to set the default-state Image for UIButton?

i am totally confused.
I just want to set an Image for the Default-State of a list of UIButtons. But in does not work.
I simply assigned the image to all my Buttons inside the setter of my Outlet-Collection but the Image just appears if the Button is highlighted but I want that the Image always is set except the button is selected. This is my code so far :
-(void)setCardButtons:(NSArray *)cardButtons
{
UIImage * cardBackImage = [UIImage imageNamed:#"card-back.jpg"];
for (UIButton * button in cardButtons) {
[button setImage:cardBackImage forState:UIControlStateNormal|UIControlStateHighlighted];
[button setImage:nil forState:UIControlStateSelected];
}
_cardButtons = cardButtons;
}
I really dont know what i am doing wrong ? How can i set the Image of my Default-State-Button without using the IB ?
There are a few states you can use, make sure you set them correctly. Copy and paste this into Xcode and fill in your images by tabbing through.
Might be worth while refreshing your memory by reading the Apple Doc for this.
[button setImage:<# normal image #> forState:UIControlStateNormal];
[button setImage:<# selected image #> forState:UIControlStateSelected];
[button setImage:<# highlighted image #> forState:UIControlStateHighlighted];
[button setImage:<# selected and highlighted image #> UIControlStateSelected | forState:UIControlStateHighlighted];
Have you tried with only:
[button setImage:cardBackImage forState:UIControlStateNormal];

How to set button image by variable?

i am trying to set the button image by variable like this:
[collideButton setImage:[UIImage imageNamed:#"key%#.png",[getKeyboarLabel]] forState:UIControlStateNormal];
this is not working an error comes expected identifiers! i dont know where do i missed anything.
if i set image by this code:
[collideButton setImage:[UIImage imageNamed:#"keya.png"] forState:UIControlStateNormal];
it works good..
now please tell me where is the problem in uper code?
use [NSString stringWithFormat#"key%#.png", key];
NSString *imageFilename = [NSString stringWithFormat:#"key%#.png", getKeyboarLabel];
[collideButton setImage:[UIImage imageNamed:imageFilename] forState:UIControlStateNormal];

How to check a buttons image in Xcode

I am currently trying to find out, whether it is possible to check which image a button uses.
Lets say I have image1 and image2.
If the buttons image is image1 do this and if buttons image is image2 do that.
But xcode doesnt give me any autofill options....
[m1 setImage:[UIImage imageNamed: #"Penguin.png"] forState:UIControlStateNormal];
This is how i set the images. but how do i find out wheter its Penguin or Penguin2?
Just used This Code for Button Action on the basis of image
- (IBAction)checkButtonClicked:(UIButton *)sender {
if ([checkButton.currentImage isEqual:[UIImage imageNamed:#"checkbox.png"]])
[checkButton setImage:[UIImage imageNamed:#"Checked.png"] forState:UIControlStateNormal];
//do some thing here for your image1
else
[checkButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
//do some thing here for your image 2
}
Cocoa Touch was designed around the Model–View–Controller pattern, so you might want to try adopting that pattern. Instead of trying to retrieve state information – the selected penguin – from the view – the button – store it in an instance variable in your controller class.
Your code for setting the image could look like this:
self.currentImage = #"Penguin.png";
[m1 setImage:[UIImage imageNamed: currentImage] forState:UIControlStateNormal];
Then when you need to check the value:
if ([#"Penguin.png" isEqual:self.currentImage]) {
do something;
}
Checking the equality of images is done like so:
if ([[UIImage imageNamed:#"Penguin.png"] isEqual:m1.currentImage]) {
// do something
}
2 methods can be used for this:
1.
UIImage *image = self.myButton.currentBackgroundImage;
2.
myImageView.image = [myButton backgroundImageForState:myButton.state];
UIImage *img=[(UIButton *) sender currentImage];
if(img == [UIImage imageNamed:#"edit"])
{
//If do something
}`
try this hope it will work
In Swift also possible
iOS 8+
if button.currentImage?.isEqual(UIImage(named: "Penguin.png")) {
//do something here
}
iOS 7-
if timerButton.currentImage == UIImage(named: "Penguin.png") {
//do something here
}

Resources