0

rect UIButtonを丸めて影を追加したいのですが、背景画像の影が表示されないように設定すると、背景が影に重なると思います。背景画像なしで影が見える

customBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    customBtn.frame=CGRectMake(customBtn.frame.origin.x, customBtn.frame.origin.y, 30, 30);
     [customBtn setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
     [customBtn setBackgroundImage:[[UIImage imageNamed:@"bg.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forState:UIControlStateNormal];
    customBtn.layer.cornerRadius=9;

 customBtn.layer.shadowColor = [UIColor redColor].CGColor;
    customBtn.layer.shadowOpacity = 1.0;
    customBtn.layer.shadowRadius = 1;
    customBtn.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
customBtn.clipsToBounds=YES;
4

1 に答える 1

1

の内部構造によりUIButton、最善の策は、影を含む伸縮可能な背景画像を作成することです。

UIImage *bgWithShadow = [[UIImage imageNamed: @"bg.png"] 
                            stretchableImageWithLeftCapWidth: 10 
                                                topCapHeight: 0];
[customBtn setBackgroundImage: bgWithShadow 
                     forState: UIControlStateNormal];

bg.pngこの例では、が影を含む21ピクセル幅の画像であると想定しています。clipsToBounds基本的に、ボタンのレイヤーにアクセスしたり、そのプロパティを変更したりするなど、他に何もする必要はありません。この質問を参照して、伸縮可能な画像が何であるかを理解してください(写真付き!)。

stretchableImageWithLeftCapWidth:topCapHeight:を優先して非推奨になっていることに注意してくださいresizableImageWithCapInsets:

于 2012-05-04T05:01:22.727 に答える