1

ボタンの背景を描画し、insertSubLayer behind: メソッドを使用してサブレイヤーとして追加する場合を除いて、カスタム UIButton クラスを作成しようとしていますが、UIButton Textlabel の前に表示されます。

私のコードは以下に掲載されています。

CALayer *layer = self.layer;

layer.cornerRadius = 3.0f;
layer.masksToBounds = YES;
layer.borderWidth = 1.0f;
layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.5f].CGColor;
self.titleLabel.textColor = [UIColor greenColor];
//layer.backgroundColor = [UIColor greenColor].CGColor;

bgColor = [CAGradientLayer layer];
bgColor.frame = self.layer.bounds;
self.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
bgColor.colors = [NSArray arrayWithObjects:
                     (id)[UIColor colorWithWhite:0.97f alpha:1].CGColor,
                     (id)[UIColor colorWithWhite:0.87f alpha:1].CGColor,
                     nil];
bgColor.locations = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0f],
                        [NSNumber numberWithFloat:1],
                        nil];
[self.layer addSublayer:bgColor];
[self.layer insertSublayer:bgColor below:layer];
4

1 に答える 1

2

self.layerコード内でlayer同じオブジェクトを指します。レイヤーの背後にサブレイヤーを挿入するように要求していますが、これは不可能です。サブレイヤーは、親レイヤー内に含まれています。試す

[self.layer insertSublayer:bgColor atIndex:0];

それ以外の

[self.layer addSublayer:bgColor]; 
[self.layer insertSublayer:bgColor below:layer];

これにより、ボタンのレイヤー階層の可能な限り低いポイントにグラデーションが追加されます。

于 2012-07-31T10:52:14.607 に答える