Hello I have the following code and one custom UIButton
class I created.
myButton.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
myButton.layer.cornerRadius = 5;
How can i use this code in my custom UIButton
class and use this code throughout all my buttons instead of duplicating this code for every single UIButton
I create in my project.
You can create your custom class inherit UIButton
class. On this class you can custom your property like you did, and use this class for everywhere you want.
@interface YourButton : UIButton
You can create Category of UIButton in Objective C.
@interface UIButton ( ExtendButton )
+(UIButton *) myButton;
@end
@implementation UIButton
+(UIButton *) myButton {
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
theButton.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
theButton.layer.cornerRadius = 5;
return theButton;
}
@end
Create new NSObjectClass for name custombuttonClass
custombuttonClass.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface custombuttonClass : NSObject
+(void)custombutton : (UIButton *)btn;
@end
custombuttonClass.m
#import "custombuttonClass.h"
@implementation custombuttonClass
+(void)custombutton : (UIButton *)btn
{
btn.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
btn.layer.cornerRadius = 5;
btn.clipsToBounds=YES;
}
@end
import custombuttonClass where to you use and call this method Example :
#import "ViewController.h"
#import "custombuttonClass.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[custombuttonClass custombutton:myButton]; // add your button as a parameter
// Do any additional setup after loading the view, typically from a nib.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.