Custom Button not showing my images for pushed state - iphone

In code, I set the following:
btnLogo.SetImage(UIImage.FromBundle("images/Zouak_logo_button.png"), UIControlState.Normal);
btnLogo.SetImage(UIImage.FromBundle("images/Zouak_logo_button_pushed.png"), UIControlState.Selected);
I couldn't find a UIControlState.Pressed or Pushed or anything along those lines. When the button is pushed of course it isn't showing the version of the image that I want. Do I need to do this in the Click event manually?

I think your syntax is kind of strange, but I guess that you want to do:
UIButton *btn = [[UIButton alloc] init];
[btn setImage:uiimg forState:UIControlStateHighlighted];

Do this for all states:
UIImage *newNormalImage = [UIImage imageNamed:#"images/Zouak_logo_button.png"];
[btnLogo setBackgroundImage:newNormalImage forState:UIControlStateNormal];
// Image for highlighted state
UIImage *newHighlightedImage = [UIImage imageNamed:#"mages/Zouak_logo_button_pushed.png"];
[btnLogo setBackgroundImage:newHighlightedImage forState:UIControlStateHighlighted];
// Image for selected state
UIImage *newSelectedImage = [UIImage imageNamed:#"mages/Zouak_logo_button_pushed.png"];
[btnLogo setBackgroundImage:newSelectedImage forState:UIControlStateSelected];

Related

problem with background image of Bar Button Item

I have a Bar Button Item and I wanna set a background image for it.
Both the button and the background image are set in the xib file.
Here how it looks like:
As you can see the image doesn't covers the whole width of the button.
Now, I tried setting up as background a larger image and here is how the result looks like:
The button gets larger itself and still the image doesn't fits its size.
Anyone any idea of how to make this work?
Try this, it works very well in these cases:
UIImage *btnImg = [UIImage imageNamed:#"myImg.png"];
UIImage *btnGreyImg = [btnImg stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[myBtn setBackgroundImage:btnGreyImg forState:UIControlStateNormal];
Of course, you can write this in one line or two lines, I just wanted to show it clearly...
In my case, I made the outlet of the BarButtonItem as backBack and in viewDidLoad: method I wrote this code:
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 250, 300);
[btnBack setImage:[UIImage imageNamed:#"sample.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(callMethod:) forControlEvents:UIControlEventTouchUpInside];
backButton.customView = btnBack;
I hope this is helpful in your case.

Different states for UIButton not working

I have written this code to see different image states...
UIButton *btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
btnComment.tag=indexPath.row;
[btnComment addTarget:self action:#selector(goToComment:)forControlEvents:UIControlEventTouchDown];
UIImage *img1 = [UIImage imageNamed:#"commentbtndown.png"];
UIImage *img2 = [UIImage imageNamed:#"commentbtnup.png"];
UIImage *img3 = [UIImage imageNamed:#"commentbtnover.png"];
[btnComment setImage:img1 forState:UIControlStateNormal];
[btnComment setImage:img2 forState:UIControlStateHighlighted];
[btnComment setImage:img3 forState:UIControlStateSelected];
[btnComment setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
btnComment.frame =CGRectMake(0, 100, 95, 25);
[cell addSubview:btnComment];
[img1 release];
[img2 release];
[img3 release];
but its not working, it is always showing me image 1.
p.s. I have added these images in the table view cell
The problem is that you are creating the UIImage objects with an autorelease method imageNamed, and you are releasing these objects afterwards, which cause your button to have invalid objects and because of that the images will not be displayed
Try removing this lines of code and your button will work
[img1 release];
[img2 release];
[img3 release];
And also, if you want the button to receive the touch events, you will have to add it to the contentView of your cell object, otherwise the button will be shown but you will not be able to tap it.
[cell.contentView addSubview:btnComment]
Well one problem with your code is that you should not be releasing those image variables. imageNamed: returns an autoreleased UIImage. I'm doubt that this is causing your problem, though.
Try using | instead of + for your fourth setImage call.
Another problem with your code is where you add the button to your UITableViewCell. Instead of this:
[cell addSubview:btnComment];
You should add subviews to your cell's contentView:
[cell.contentView addSubview:btnComment];
But I'm also not certain that this may cause your problem...

Temporarily change a UIButtons image

I have a UIButton which I assign an image in IB.
I then change the image in the code, but I only want this to be temporary.
How does one return the button to the image set in IB or is there a way to revert back after doing the temporary switch?
Thanks
If you want to revert back to the previous image you would have to store a reference to it somewhere:
// prevImage is an ivar UIImage *
prevImage = [[myButton imageForState:UIControlStateNormal] retain];
// Change to the new image
...
// Later: revert back to prevImage
[myButton setImage:prevImage forState:UIControlStateNormal];
[prevImage release];
prevImage = nil;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateDisabled];
You can set your own custom images for different states. when you tap on it, the corresponding images will appear on your button.

UIScrollView clickable images iPhone

I have created a UIScrollView in my app - adapted from this sample code: http://developer.apple.com/iphone/library/samplecode/Scrolling/Introduction/Intro.html
But I have no idea how to make the images clickable
Any suggestions please?
Thanks!
Yeah... "Put them on UIButtons"
//get the image to use, however you want
UIImage* image = [product.images objectAtIndex:i];
UIButton* button = [[UIButton alloc] init];
//set the button states you want the image to show up for
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
//create the touch event target, i am calling the 'productImagePressed' method
[button addTarget:self action:#selector(productImagePressed:)
forControlEvents:UIControlEventTouchUpInside];
//i set the tag to the image #, i was looking though an array of them
button.tag = i;
//add the new button w/ the image to your scrollview
[ImagesScroller addSubview:button];
[button release];
Put them on UIButtons.

Why does my UIButton.titleLabel.text disappear when I place an image in the button?

I've had this problem before but was able to work around it until now, Basically I'm creating a custom UIbutton setting its image as a uiimage and then the button that has had a label until I implimented the below code now loses its label. I need that label because it is set programatically in code that follows.
NSString *imageName = [NSString stringWithFormat:kNameOfButtonimage ];
UIImage *image = [UIImage imageNamed:imageName];
[button setImage:image forState:UIControlStateNormal ];
Any help you could lend would be mucho appreciated.
-nick
I needed to do a:
[button setBackroundImage:image forState:UIControlStateNormal ];
instead of this:
[button setImage:image forState:UIControlStateNormal ];
;)