Reputation: 361
I would like to create a flat button with a background color of #E74C3C
Here is the code I have so far:
[UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
Upvotes: 1
Views: 250
Reputation: 48524
You are new to iOS development? Do things right from the get go:
Upvotes: 2
Reputation: 1203
You will want your button in your storyboard first. Then, in your ViewController
file, ensure that you have an IBOutlet
for your button.
in your .h file under interface
@property (strong, nonatomic) IBOutlet UIButton *myButton;
in your .m file under implementation
@synthesize myButton;
Once you have this defined as a property, you can set the colour of your button in your viewDidLoad
or viewDidAppear
like so:
myButton.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
or
[myButton setBackgroundColor:[UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1]];
Once you're done this, make sure to connect the outlet in your storyboard: information on that is available in the Apple Documentation.
For rounding the corner of the button, just do the following:
myButton.layer.cornerRadius = 5;
Upvotes: 3
Reputation: 1739
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
btn.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
[btn setTitle:@"Test" forState:UIControlStateNormal];
[self.view addSubview:btn];
Upvotes: 4