I've tried creating a UIButton with UIButtonTypeCustom. Then afterwards used the following methods to adjust the look of it
[sendButton setImage:[UIImage imageNamed:#"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setImage:[UIImage imageNamed:#"sendButtonOn.png"] forState:UIControlStateHighlighted];
[sendButton setImage:[UIImage imageNamed:#"sendButtonDisabled.png"] forState:UIControlStateDisabled];
However the problem is, once you start setting UIImages to the states, setTitle no longer works. I think setTitle only works with non-UIButtonTypeCustom? This means I either have to put the text into the images themselves (not very robust) or subclass this and add a UILabel to the view? Sounds like alot of work :( Any ideas?
If you want the title to display over the image, try using this:
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonOn.png"] forState:UIControlStateHighlighted];
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonDisabled.png"] forState:UIControlStateDisabled];
Related
I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties here...
UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(165, 205, 65, 40)];
[myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal];
[myButton setTitle:#"Hi....." forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
[cell addSubview:myButton];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
In my app am using UIScrollView. I added buttons to UIScrollView.
For each button i assigned image using
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
and actions.
forState i used UIControlStateNormal in viewDidLoad.
And when we click on button image has to change.
For this i used same method but forState value changed to UIControlStateSelected in ViewDidLoad.
But button image is not changing.
any one can help or suggest
You've to use like this
In ViewDidLoad Set images for normal and selected state
-(void)ViewDidLaod
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateSelected];
To perform selection image change can be done below code
-(void)buttonclick
[myButton setSelected:YES];
or
[myButton setSelected:NO];
Use this line of code I think this will be helpful for you.
-(void)viewDidLoad
{
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
}
-(void)compete
{
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateNormal];
}
use
[button setImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"button_highlighed.png"] forState:UIControlStateHighlighted];
I want to make my UIButton look like this:
How can I achieve this look? Is there a built in setting or will I just have to manually create it in photoshop?
One option is to just use an image for the background, like this one:
There's also a class called GMButton that is found on github. You can set the title, color, titleColor for each state. Just use it in place of UIButton (it's a subclass of UIButton). With it you can create buttons like these or these:
For your Delete button, I would try this:
GMButton* button = [GMButton buttonWithFrame:CGRectMake(18,3,284,34)];
button.titleLabel.font = [UIFont systemFontOfSize:17];
[button setTitle:#"Delete" forState:UIControlStateNormal];
[button setColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
It uses 3 layers to create the button. A CAGradientLayer for the bevel, a CALayer for the color, and a CAGradientLayer for the highlighting.
Use a uibutton of type custom, and provide an image for each state.
UIButton *myDeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myDeleteButton setImage:[UIImage imageNamed:#"ic_button_titlebar_delete.png"] forState:UIControlStateNormal];
[myDeleteButton setImage:[UIImage imageNamed:#"ic_button_titlebar_delete_on.png"] forState:UIControlStateSelected];
So, yes, you'll need to use PhotoShop or some other imaging tool.
For UIButton, there is no built in setting to achieve this. You could either code it yourself with gradients, or create custom images for the button's states. The only built in option to get this button is via an UIActionSheet.
I set a number of UIButtons in IB. When I try to set them in the code using
[A1 setImage:[UIImage imageNamed:#"t1A.png"] forState:UIControlStateNormal];
This does not set the image. Could it be that the button wont change if it is set in IB?
Thanks
Try this
[A1 setBackgroundImage:[UIImage imageNamed:#"t1A.png"] forState:UIControlStateNormal];
Apple Docs:
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
Try this:
[YourButton setBackgroundImage:[UIImage imageNamed:#"XYZ.png"] forState:UIControlStateNormal];
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.