2017-01-11 8 views
0

私は毎日変更されるボタンにテキストを書き込むために複数のラベルが必要になるアプリがあります。これを行うには、UIButtonのサブクラスを設定していますが、問題があります。サブクラスの実装は次のようになります。UIButtonのサブクラスが機能しない

@implementation GlossyButton 


- (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self makeButtonShiny:self withBackgroundColor:backgroundColor]; 
    } 
    return self; 
} 

- (void)makeButtonShiny:(GlossyButton*)button withBackgroundColor:(UIColor*)backgroundColor 
{ 
    // Get the button layer and give it rounded corners with a semi-transparant button 
    UIColor *bobcatred = [UIColor whiteColor]; 

    CALayer* l = button.layer; 

    CAGradientLayer *shineLayerl = [CAGradientLayer layer]; 
    shineLayerl.frame = l.bounds; 
    shineLayerl.colors = [NSArray arrayWithObjects: 
          (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor, 
          (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor, 
          (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor, 
          (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor, 
          (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor, 
          nil]; 
    shineLayerl.locations = [NSArray arrayWithObjects: 
          [NSNumber numberWithFloat:0.0f], 
          [NSNumber numberWithFloat:0.5f], 
          [NSNumber numberWithFloat:0.5f], 
          [NSNumber numberWithFloat:0.8f], 
          [NSNumber numberWithFloat:1.0f], 
          nil]; 

    [l setMasksToBounds:YES]; 
    [l setCornerRadius:11]; 
    [l setBorderWidth:2.0]; 
    [l setBorderColor: [bobcatred CGColor]]; 
    [l addSublayer:shineLayerl]; 
    // Add the layer to the button 
    [button.layer addSublayer:shineLayerl]; 

    [button setBackgroundColor:[UIColor redColor]]; 
} 

@end 

私はラベルを追加することなく、すべてを適切に描画することを確認します。

次に、これが表示されるViewControllerに行き、ボタンのIBOutletプロパティを追加します。ストーリーボードでは、UIButtonをビューにドラッグし、プロパティの型をカスタムに変更し、クラスIではUIButtonからGlossyButtonに変更します。私はそれを実行しますが、グラデーション、境界線、または何もなしでアプリ内に空白があります。私は間違って何をしていますか?ストーリーボードからインスタンス化カスタムUIButtonのサブクラスの

+0

イニシャライザが呼び出されるようにする必要があります。 –

+0

'GlossyButton'のインスタンスを表示してください。 – shallowThought

+0

ジェームスは同意します...私はストーリーボード上のアイテムの初期化子は 'initWithCoder'だと思います。 – Samantha

答えて

3

initWithFrameは呼び出されません。代わりにinitWithCoderを試してください。

+0

SMH、ありがとう。 – user717452

+0

問題はありません、それはstackoverflowのためのものです:-) – auspicious99

関連する問題