Mr.Milannist
Mr.Milannist

Reputation: 39

Modify button's style iOS

I have a default iOS UIButton (in storyboard).

I want to change it (delete border-radius, add solid border, change background etc.). Where should I should write the code? Which method should I use? Which classes should I import?

Upvotes: 0

Views: 4974

Answers (3)

Marius Kurgonas
Marius Kurgonas

Reputation: 83

You need to change your property definition to this:

@property (nonatomic, strong) IBOutlet UIButton *rssButton;

And connect your UIButton object from storyboards to this property then you will be able to change object as you like though code (assuming you are doing this in the same controller)

And remove your last line where you are adding that button to your view if it is already defined in your storyboard

Upvotes: 0

Mike D
Mike D

Reputation: 4946

You can do most, if not all, of those by choosing custom for button type in Interface Builder. If that does not accomplish everything, you can set all this via code:

Make sure you have #import <QuartzCore/QuartzCore.h> in your .m file.

Set properties (base in this SO answer):

float borderWidth = ...;
UIColor *borderColor = ...; // create the color you want

[[myButton layer] setBorderWidth:borderWidth];
[[myButton layer] setBorderColor:borderColor.CGColor];

You can go through all the button properties you want in a similar way. (

Upvotes: 2

edzio27
edzio27

Reputation: 4140

In my simple code i have something like this:

@property (nonatomic, strong) UIButton *rssButton;

Then i define this button in implementation:

- (UIButton *)rssButton {
    if(_rssButton == nil) {
        _rssButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _rssButton.frame = CGRectMake(60, 200, 200, 40);
        [_rssButton setTitle:@"Get newest RSS article" forState:UIControlStateNormal];
        _rssButton.titleLabel.textColor = [UIColor colorWithRed:0.294 green:0.553 blue:0.886 alpha:1];
        _rssButton.backgroundColor = [UIColor whiteColor];

        _rssButton.layer.borderColor = [UIColor blackColor].CGColor;
        _rssButton.layer.borderWidth = 0.5f;
        _rssButton.layer.cornerRadius = 10.0f;

        [_rssButton addTarget:self action:@selector(getDataFromRSS) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rssButton;
}

And next add in in your main view:

[self.view addSubview:self.rssButton];

Upvotes: 0

Related Questions