2017-05-08 14 views
1

가이 코드에 사용자 지정있는 UIButton을 만들어 작동하지 않습니다아이폰 OS 설계 가능 단추 배경 색상

@implementation HGButton 

- (instancetype)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) { 
     [self initVariables]; 
     [self customInit]; 
    } 

    return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self initVariables]; 
     [self customInit]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [self customInit]; 
} 

- (void)setNeedsLayout 
{ 
    [super setNeedsLayout]; 
    [self setNeedsDisplay]; 
} 

- (void)prepareForInterfaceBuilder 
{ 
    [self customInit]; 
} 

-(void) initVariables 
{ 
    self.fillColor   = [UIColor lightGrayColor]; 
    self.borderWidth  = 2; 
    self.cornerRadious  = 10; 
} 

- (void)customInit 
{ 
    [self setBackgroundColor:self.fillColor]; 

    self.layer.cornerRadius = self.cornerRadious; 
    self.layer.borderWidth = self.borderWidth; 

    if (self.cornerRadious > 0) { 
     self.layer.masksToBounds = YES; 
    } 
} 

그것은 아주 간단한 코드입니다. 스토리 보드의보기에 내 버튼을 추가하면 완벽 해 보입니다. 그러나 앱을 재생할 때 버튼 배경색은 항상 밝은 회색입니다. 버튼을 누르면 배경색이 스토리 보드에서 선택한 색상으로 변경됩니다.

왜 이렇게할까요? 오류는 어디에 있습니까?

답변

1

self.fillcolor이 스토리 보드를 통해 변경되었을 때 배경색을 업데이트하지 않으므로. 당신을 가정

@property (nonatomic, copy) IBInspectable UIColor *fillcolor; 

이 클래스에서이 도움이

- (void)setFillColor:(UIColor *)fillColor { 
    if (fillColor != _fillColor) { 
     _fillColor = fillColor; 
     [self customInit]; 
    } 
} 

희망을 다음 코드를 추가합니다.

건배.

+0

덕분에 수업이 추가) –

0

fillColor를 모니터링하기 위해 'set method'를 작성해야합니다.

- (void)setFillColor:(UIColor *)fillColor 
{ 
    _fillColor = fillColor; 
    self.backgroundColor = fillColor; 

} 
0

지금은 작동

- (void)setFillColor:(UIColor *)fillColor { 
    self.backgroundColor = fillColor; 
}