#define kBtnBackgroundColor [UIColor colorWithRed:73.0/255.0 green:189.0/255.0 blue:204.0/255.0 alpha:1.0]
#define kBtnBackgroundColor1 [UIColor colorWithRed:73.0/255.0 green:200.0/255.0 blue:30.0/255.0 alpha:1.0]
#define kBtnFont [UIFont fontWithName:@"arial" size:17]
//純色圓角按鈕1
UIButton *btnLogin = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btnLogin.frame =CGRectMake(30.0,275.0, 260.0,45.0);
[btnLogin setBackgroundColor:kBtnBackgroundColor];
btnLogin.layer.cornerRadius =4.0;//關鍵點,設置btn的圓角大小
btnLogin.titleLabel.font =kBtnFont;
[UIFontfontWithName:@"arial"size:17];
[btnLogin setTitle:@"ok"forState:UIControlStateNormal];
[btnLogin setTintColor:[UIColorwhiteColor]];//關鍵點,設置btn的文字顏色
[btnLogin addTarget:selfaction:@selector(changePw:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnLogin];
//純色圓角按鈕2
UIButton *btnLogin1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btnLogin1.frame =CGRectMake(30.0,200.0, 260.0,45.0);
[btnLogin1 setBackgroundColor:kBtnBackgroundColor1];
btnLogin1.layer.cornerRadius =4.0;
btnLogin1.titleLabel.font =kBtnFont;
[UIFontfontWithName:@"arial"size:17];
[btnLogin1 setTitle:@"ok"forState:UIControlStateNormal];
[btnLogin1 setTintColor:[UIColorwhiteColor]];
[btnLogin1 addTarget:selfaction:@selector(changePw:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnLogin1];
實戰
項目中多個地方用到了自定義的圓角矩形,但是我想用XIB來實現自定義的按鈕的創建
於是重寫UIButton
內容:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self == [super initWithCoder:aDecoder])
{
[self initlization];
}
return self;
}
- (void)initlization
{
[self setBackgroundColor:kBtnBackgroundColor];
self.layer.cornerRadius = 4.0;//關鍵點,設置btn的圓角大小
[self setTintColor:[UIColor blackColor]];//關鍵點,設置btn的文字顏色
}
在使用時,直接用XIB來拖一個按鈕,
使用即可,
現在還有一個問題,那個- (id)initWithCoder:(NSCoder *)aDecoder 方法不點明白,是什麼時候纔會用到的呢?