你好,我有以下代碼和我創建的一個自定義類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的自定義類 –