How would I change the background color of a button to black?
-
Read this: http://stackoverflow.com/questions/2808888/is-it-even-possible-to-change-a-uibuttons-background-color – Johannes Lumpe May 08 '12 at 06:26
-
see this thread http://stackoverflow.com/questions/2808888/is-it-even-possible-to-change-a-uibuttons-background-color/2809237#2809237 – Saad May 08 '12 at 06:35
7 Answers
If you want to programatically change the colour of button you will have to make the type of button as custom as :-
UIButton *button = [UIButton buttonWithType:uibuttontypecustom];
note "uibuttontypecustom"
and then you can change backgroung by [btn setBackgroundColor:[UIColor blackColor]];
or you can set a black backgroung image by using steImage
method on btn
.

- 277
- 3
- 10

- 6,068
- 1
- 23
- 25
I think this will help you,
[button setBackgroundColor:[UIColor redColor]];

- 129,200
- 40
- 280
- 281

- 158
- 1
- 7
Mr/Ms Baiju please review below my code,
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 200, 300, 40);
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor blackColor]];
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[self.view addSubview: button];
I think it will help you to set background color and set background image for a button. If you want to set image please change the button type RoundedRect to Custom
. I hope it will help you little bit. Thanks.

- 9,741
- 16
- 71
- 100
Use this line of code and try use any color image .
[myButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

- 750
- 7
- 14
-
is this really the only way to do it? if so, xcode is greatly lacking in options. although many of us already knew that – Katushai Mar 23 '14 at 18:13
If anyone wants to know how to make same thing in swift with full code then here it is...Just write this code in your ViewController.swift
import UIKit
class ViewController: UIViewController
{
var firstbutton:UIButton? //firstbutton = Your Button Name
override func viewDidLoad() {
super.viewDidLoad()
self.firstbutton = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
self.firstbutton!.frame = CGRectMake(100, 200, 100, 100)
//This Line is used for color of your button
self.firstbutton!.backgroundColor = UIColor.redColor()
self.view.addSubview(firstbutton!)
}
//This section is used to bring the button in front of everything on the view
override func viewDidAppear(animated: Bool) {
self.view.bringSubviewToFront(self.firstbutton!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

- 18,820
- 42
- 108
- 144

- 6,500
- 4
- 28
- 37
in your XIB drag and drop a button. got to button properties in your right hand side and set button type to custom. (Or you can do than in code too). In your .h file create UIButton * btn;
and bind it to the XIB.
Then go to .m file and in viewdidload method right this code.
[btn setBackgroundColor:[UIColor blackColor]];
Thanks :)

- 3,638
- 3
- 27
- 48
-
this is the same answer as above, and it didnt work when i tried it his way either. also, the question was asking how to do it programmatically. that implies no use of IB at all – Katushai Mar 23 '14 at 18:13
Change the layer colour e.g. self.btn.layer.backgroundColor = [UIColor redColor].CGColor;

- 63
- 6