Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a button background image iPhone programmatically

In Xcode, how do you set the background of a UIButton as an image? Or, how can you set a background gradient in the UIButton?

like image 676
Linda Avatar asked Sep 16 '10 04:09

Linda


People also ask

How do I change the button background in Xcode?

Just scroll a little bit in Attribute Inspector , you can find Background property in view section that will allow you set the backgroundColor for the UIButton . If you want to set backgroundColor of button programatically.


6 Answers

Complete code:

+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    // in case the parent view draws with a custom color or gradient, use a transparent color
    button.backgroundColor = [UIColor clearColor];

    return button;
}

UIImage *buttonBackground = UIImage imageNamed:@"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:@"blueButton.png";

CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);

UIButton *button = [FinishedStatsView buttonWithTitle:title
                                               target:target
                                             selector:action
                                                frame:frame
                                                image:buttonBackground
                                         imagePressed:buttonBackgroundPressed
                                        darkTextColor:YES];

[self addSubview:button]; 

To set an image:

UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];

To remove an image:

[button setBackgroundImage:nil forState:UIControlStateNormal];
like image 195
Dr. Rajesh Rolen Avatar answered Sep 27 '22 01:09

Dr. Rajesh Rolen


This will work

UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
like image 27
Pathetic Learner Avatar answered Sep 25 '22 01:09

Pathetic Learner


In case it helps anyone setBackgroundImage didn't work for me, but setImage did

like image 42
RanLearns Avatar answered Sep 28 '22 01:09

RanLearns


You can set an background image without any code!

Just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

like image 31
JELLYFUN Avatar answered Sep 28 '22 01:09

JELLYFUN


Swift

Set the button image like this:

let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)

where myImageName is the name of your image in your asset catalog.

like image 40
Suragch Avatar answered Sep 26 '22 01:09

Suragch


When setting an image in a tableViewCell or collectionViewCell, this worked for me:

Place the following code in your cellForRowAtIndexPath or cellForItemAtIndexPath

// Obtain pointer to cell. Answer assumes that you've done this, but here for completeness.

CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];

// Grab the image from document library and set it to the cell.

UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];

NOTE: xCode seemed to get hung up on this for me. I had to restart both xCode and the Simulator, it worked properly.

This assumes that you've got cheeseThumbnail set up as an IBOutlet... and some other stuff... hopefully you're familiar enough with table/collection views and can fit this in.

Hope this helps.

like image 29
kmiklas Avatar answered Sep 24 '22 01:09

kmiklas