system21
system21

Reputation: 361

Create UI Flat button for objective c

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

Answers (3)

SwiftArchitect
SwiftArchitect

Reputation: 48524

0 lines of code

Picker

You are new to iOS development? Do things right from the get go:

  1. Use Interface Builder
  2. Use Storyboard
  3. Take advantage of technology that is available to you, do not program on your hands and knees.

IB

Upvotes: 2

Leejay Schmidt
Leejay Schmidt

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

Jaydeep Patel
Jaydeep Patel

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

Related Questions