带圆角和阴影的iOS按钮

带圆角和阴影的iOS按钮,ios,objective-c,Ios,Objective C,我已经寻找并尝试了各种解决方案,但没有成功。我想要的是一个圆角和阴影的按钮背景。我可以让一个或另一个发生,但不能两者同时发生。欢迎任何帮助 viewDepositButton_.layer.cornerRadius = 5.0; CAGradientLayer *viewLayer = [CAGradientLayer layer]; [viewLayer setColors:aloColors]; [viewLayer setFrame:viewDepositButton_.bounds];

我已经寻找并尝试了各种解决方案,但没有成功。我想要的是一个圆角和阴影的按钮背景。我可以让一个或另一个发生,但不能两者同时发生。欢迎任何帮助

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);

下面是我设置带阴影的圆角按钮的代码

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;

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

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

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;

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

我可以通过以下步骤使您的代码正常工作,而且看起来还不错。确保按正确的顺序调用方法

        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];}

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

        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];}

viewDepositButton\uClipsToBounds=YES或<代码>viewDepositButton_uu.layer.masksToBounds=是
将剪辑层外的所有内容-包括阴影

但是,您几乎没有选择:

  • 将按钮放在父视图下,该视图具有相同的角半径和所需的阴影,但将具有
    clipsToBounds=NO
    。这样,按钮将作为具有阴影的视图的子视图
  • 使用带有圆角的按钮图像,并将按钮设置为
    clipstobunds
    NO

  • 希望这有帮助

    viewDepositButton\uz.clipsToBounds=YES或<代码>viewDepositButton_uu.layer.masksToBounds=是
    将剪辑层外的所有内容-包括阴影

    但是,您几乎没有选择:

  • 将按钮放在父视图下,该视图具有相同的角半径和所需的阴影,但将具有
    clipsToBounds=NO
    。这样,按钮将作为具有阴影的视图的子视图
  • 使用带有圆角的按钮图像,并将按钮设置为
    clipstobunds
    NO

  • 希望这有帮助

    您的代码有什么问题吗?我会在您设置属性后调用您的方法![ViewDepositorButton_u2;.layer insertSublayer:viewLayer atIndex:0];[viewDepositButton_uViewLayer];}应该稍后再调用。请看看你的代码有什么问题?我可能会在你设置属性后调用你的方法![ViewDepositorButton_u2;.layer insertSublayer:viewLayer atIndex:0];[viewDepositButton_uViewLayer];}应该在以后被称为方式。请看它的第一眼,但如果你仔细看按钮的角落不是圆形的,并延伸出边界。现在我想我可能不得不放弃渐变,只使用普通的颜色。我已经找到了一种方法让它工作。我需要对上面的代码做的是删除边框,因为我不希望这样,然后添加[viewLayer setCornerRadius:5.0f];当定义渐变层时。乍一看,这是可行的,但是如果你仔细观察按钮,角不会变圆,并且延伸出边界。现在我想我可能不得不放弃渐变,只使用普通的颜色。我已经找到了一种方法让它工作。我需要对上面的代码做的是删除边框,因为我不希望这样,然后添加[viewLayer setCornerRadius:5.0f];定义渐变层时。