iOS按钮带有圆角和阴影

时间:2016-07-07 17:06:01

标签: ios objective-c

我搜索并尝试了各种解决方案但没有成功。我想要的是带有圆角和阴影的按钮背景。我可以让一个或另一个发生,但不能同时发生。任何帮助都会非常受欢迎。

viewDepositButton_.layer.cornerRadius = 5.0;
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
viewDepositButton_.clipsToBounds = YES;

viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);

3 个答案:

答案 0 :(得分:4)

10000000.toString(); #!/bin/sh cat Input.Format Log.File | awk 'BEGIN { Month = " JanFebMarAprMayJunJulAugSepOctNovDec" } { if (NR == 1) { startm = index(Month, $2) / 3 if (length(startm) == 1) { startm = "0" startm } startm = $4 startm $3 endm = index(Month, $6) / 3 if (length(endm) == 1) { endm = "0" endm } endm = $4 endm $3 # print startm " " endm } else { logdate = substr($1,8,2) substr($1,5,2) substr($1,2,2) # print logdate if (logdate >= startm ) { prtsw = 1 } if (logdate > endm ) { prtsw = 0 } if (prtsw == 1 ) { print $0 } } }' 会剪切图层外的所有内容 - 包括阴影。

但是你几乎没有选择:

  1. 将您的按钮放在父视图下,该视图将具有相同的角半径和所需的阴影,但将具有viewDepositButton_.clipsToBounds = YES;。这样,您就可以将按钮作为具有阴影的视图的子视图。
  2. 为按钮使用图片,该按钮具有圆角并将按钮viewDepositButton_.layer.masksToBounds = YES;设置为clipsToBounds = NO
  3. 希望这有帮助

答案 1 :(得分:2)

我能够通过以下方式让你的代码工作,看起来很好。确保按正确顺序调用方法。

        viewDepositButton_.layer.cornerRadius = 5.0;
        viewDepositButton_.layer.borderWidth = 1.0;
        viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
        viewDepositButton_.layer.shadowRadius = 8;
        viewDepositButton_.layer.shadowOpacity = 0.8;
        viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);

        CAGradientLayer *viewLayer = [CAGradientLayer layer];
        [viewLayer setColors:aloColors];
        [viewLayer setFrame:viewDepositButton_.bounds];
        [viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
        [viewDepositButton_ viewLayer];}

答案 2 :(得分:1)

这是我设置带阴影的圆角按钮的代码。

button.layer.cornerRadius = 15;
button.layer.shadowRadius = 2.0f;
button.layer.shadowColor = [UIColor lightGrayColor].CGColor;
button.layer.shadowOffset = CGSizeMake(-1.0f, 3.0f);
button.layer.shadowOpacity = 0.8f;
button.layer.masksToBounds = NO;

总的来说,代码非常简单。如果您有任何问题,疑虑或错误,请发表评论。