你好,我有以下代码和我创建的一个自定义类UIButton
。UIButton自定义类
myButton.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
myButton.layer.cornerRadius = 5;
如何使用这个代码在我的自定义UIButton
类,并使用此代码在所有我的按钮,而不是重复此代码为每一个UIButton
我在我的项目创建。
你好,我有以下代码和我创建的一个自定义类UIButton
。UIButton自定义类
myButton.backgroundColor = [UIColor colorWithRed: 0.906 green: 0.298 blue: 0.235 alpha: 1];
myButton.layer.cornerRadius = 5;
如何使用这个代码在我的自定义UIButton
类,并使用此代码在所有我的按钮,而不是重复此代码为每一个UIButton
我在我的项目创建。
您可以创建自定义类继承UIButton
类。在这个课堂上,你可以像你一样定制你的财产,并在任何你想要的地方使用这个课程。
@interface YourButton : UIButton
我真的不明白我是新的客观的C – user3004527
可以在目标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
的名称custombuttonClass创建新NSObjectClass
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
进口custombuttonClass:
#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.
}
使用您的自定义UIButton子类而不是UIButton。 –
你可以继承一类UIButton,并且你可以将这些行添加到该类中,而不是使用mybutton,你可以使用self.backgroundcolor并将该类添加到故事板中的按钮 –
只需将此代码粘贴到自定义UIButton类中并继承你所有的UIButton的自定义类 –