Stephan Kuijs
Stephan Kuijs

Reputation: 5

Alter properties for multiple buttons at once

I have this code to add borders to buttons, which has to be done to quite a few buttons. Can I do this any easier without typing it out x times?

[self.button.layer setBorderWidth:2.0];
[self.button.layer setBorderColor:[[UIColor blackColor] CGColor]];
[self.button.layer setCornerRadius:5.0];

Upvotes: 0

Views: 67

Answers (1)

Buntylm
Buntylm

Reputation: 7373

For adding Multiple Property You can create Custom UIButton class. like create an class inherited UIButton like in .h

#import <UIKit/UIKit.h>

@interface SLLabel : UIButton

@end

and add all required property into the implementation file like.

@implementation SLLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    //add your proerty here
    [self.button.layer setBorderWidth:2.0];
    [self.button.layer setBorderColor:[[UIColor blackColor] CGColor]];
    [self.button.layer setCornerRadius:5.0];
}
@end

And then use you CustomButton as you use UIButton simply #import and Code.

Thanks

Upvotes: 1

Related Questions