I want to check how to check if backgroundimage of UIbutton is present or not
I have code,
[Button setBackgroundImage:[UIImage imageNamed:#"default_module.png"] forState:UIControlStateNormal];
I want to check if there is image named "default_module.png" present and included in project folder or not.
Thanks
Just do this:
if(![UIImage imageNamed:#"default_module"]){
//The image was not present. Act accordingly.
}else{
//Continue as normal
}
Related
I am developing one application.In that i am doing comparision operation for two images like below
UIImage *actual_image=usrcheck_image.image;
NSData *present_image = UIImagePNGRepresentation(actual_image);
NSData *compare_image = UIImagePNGRepresentation([UIImage imageNamed:#"unchk-1.png"]);
if([present_image isEqualToData:compare_image])
{
set the checked image
}
else
{
set the uncheck image.
}
If i run this,check image is changed to uncheck.But uncheck image is not chneged to check image.Everytime else block is executed.So please help me how to compare these two images.This code is working perfectly in device.But the problem is in simulator only.
Try This Code -
Take 2 images of checked and unchecked and assign that images to UIButton.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 160, 30);
[button setImage:[UIImage imageNamed:#"checkedImage.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"uncheckedImage.png"] forState:UIControlStateSelected];
button.tag = 1;
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
And In buttonAction function -
selectedBtn.selected = NO;
button.selected = YES;
selectedBtn = button;
In .h file just mention
UIButton *selectedBtn;
Hope It may help you.
You can use highlighted property of UIImageView. Set the checked image as highlightedImage property, and on the click event set:
yourImageView.isHighlighed = !yourImageView.isHighlighed;
it will automatically show your image as toggle
May be link below can help you
Objective-C: Comparing an image against another image that has been previously saved
Let me know if this works
First thing is, there is no need of comparison of image Data and no need to compare images too,
But if you want to compare images means , just simply do like following
if(usrcheck_image.image==[UIImage imageNamed:#"unchk-1.png"]) {
usrcheck_image.image=[UIImage imageNamed:#"chk-1.png"];
} else {
usrcheck_image.image=[UIImage imageNamed:#"unchk-1.png"];
}
I have created a custom UIButton for an iOS 4.3+ application. I wanted to make the background image stretch, so I and used the following methods to set its background image:
[myButton setBackgroundImage:[[UIImage imageNamed:myImagePath] stretchableImageWithLeftCapWidth:leftStretchValue topCapHeight:topStretchValue] forState:UIControlStateNormal]
[myButton setBackgroundImage:[[UIImage imageNamed:myHighlightedImagePath] stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateHighlighted]
[myButton setBackgroundImage:[[UIImage imageNamed:mySelectedImagePath]
stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateSelected]
I have added an event handler to the custom button
[myButton addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
In the above mentioned methods, I have set the selected and the highlighted state of the button accordingly. I have changed the frame of the UIButton to adjust the dimensions of an irregular image (mySelectedImage and myHighlightedImage, neither of which is a rectangle), so that it gets aligned properly along with myImage.
- (void) buttonSelected {
myButton.selected = !myButton.selected; //setting the selected property to NO in the viewDidLoad
//code to change the frame of the UIButton to accomodate the irregular image
}
- (void) buttonHighlighted {
myButton.highlighted = YES;
}
The problem I am facing is that when I select myButton, a gray color overlay is displayed on myImage for a very short time before the images myHighlightedImage and mySelectedImage are displayed as its background. I have set the background color of myButton as clearColor, thinking that might be the reason, but it is not getting solved.
What could be the possible issue... ?
button.adjustsImageWhenHighlighted = NO;
I think that is what you are looking for..
I'm trying to add subviews to a UIButton. This is working fine right now. But the button isn't clickable anymore as soon as I add the subviews.
I use the following code:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
The button works again if I remove the 2 addSubview: methods. If anyone knows how to fix this it would be great!
I found a quick solutions. I needed to set the asyncImageView to the following:
asyncImage.userInteractionEnabled = NO;
asyncImage.exclusiveTouch = NO;
After this, it worked!
try:
[UIButton buttonWithType:UIButtonTypeCustom];
instead of:
[UIButton buttonWithType:UIButtonControlType];
The important thing here is to make sure that userInteractionEnabled will be set to NO. Fortunately it works immediately for UIImageView and UILabel (maybe for other subclasses of a UIView but those are the most popular subviews added to button) because by default for this classes it is set to NO by default. Unfortunately it is set to YES in UIView so make sure to change it in that case. Messing around with any other flags shouldn't be necessary. The nature of problem is that many people do not know that default value of this flag is different in subclasses.
Have you tried to put:
[asyncImage setUserInteractionEnabled:YES];
in the same sitiation i make this action:
inherit from UIButton and add all labels and imageview's of button to self, finally put new button to view as last subview and add targets of self button to this last button(also set backgroundColor to clearColor for transparent). now it will be clickable and works fine.
In Swift, test that is you have your UIButton blocked
uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
I added a label to the subview button.
For a very long time I was looking for why my text in the button is not clickable. Solution in this thread:
myLable.isUserInteractionEnabled = false
I use [button setImage:image forState:0]; to set a image to a UIButton.
Then I want the title of it using [button setTitle:[titleArray objectAtIndex:i] forState:0];.
But the image cover the title which I want to show in front of the image.
Any solution?
Thanks!
use setBackgroundImage:forState instead. When you use the (foreground) image, you dont get to set a title.
I have a button in headerview of my tableview, its woking fine as well, but problem i am having is: When I want to hide that button on click of another button. How can I achieve this? I have already tried
[deletebutton removeFromSuperview];
where deleteButton is my buttons name.
Here is what I am doing
-(void)PutTableinEditMode{
DeleteButton=[UIButton buttonWithType:UIButtonTypeCustom];
DeleteButton.frame=CGRectMake(10,1, 65, 25);
[DeleteButton setTitle:#"Delete" forState:UIControlStateNormal];
DeleteButton.backgroundColor=[UIColor clearColor];
[DeleteButton addTarget:self action:#selector(DeleteMultipleToDos) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:DeleteButton];
}
Then I m having another function as below from where i want to hide this DeleteButton
-(void)DoneEditing
{
DeleteButton.hidden=YES;
[DeleteButton removeFromSuperview];
}
deletebutton.hidden = YES
if any of those two solution (removeFromSuperview and hidden) do not work, then you certainly don't have the right reference to your button. I guess your using nib files to define your views ? Then be sure the member deleteButton is well "linked" to the actual button. (i.e. verify using debug that your deleteButton var is not null)
Can u try and change the Text of that button because i'm thinking your sending messages to nil objects.
I got the it working actually [DeleteButton removeFromSuperView] is working .. problem was that the first method PutTableinEditMode was being called from somewhere else also, so i simply took a bool variable and on basis of its value added and removed the delete button
:)
Thanks guys for helping though :)
Ifeel so stupid to make such a silly mistake :)