圆形按钮带有边框和阴影

时间:2019-06-04 08:45:02

标签: ios uibutton shadow

enter image description here

创建按钮(如图像中的按钮)的最佳方法是什么? 我尝试过:

            self.button.layer.shadowColor = UIColor.redColor.CGColor;
            self.button.layer.shadowRadius = 30.0f;
            self.button.layer.shadowOpacity = 1.0f;
            self.button.layer.shadowOffset = CGSizeMake(0.0f, 30.0f);
            self.button.clipsToBounds = NO;

            [self.button setNeedsDisplay];

但这只是添加了一个很小的非常模糊的阴影,根本不像图像中那样。

1 个答案:

答案 0 :(得分:0)

嘿@ user426132设置UIbutton背景图片并尝试使用此代码

对于 Objective-c

self.button.layer.cornerRadius = self.button.frame.size.width / 2;
self.button.layer.masksToBounds = false;
self.button.layer.shadowColor = UIColor.grayColor.CGColor;
self.button.layer.shadowOpacity = 1.0;
self.button.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.button.layer.shadowRadius = 10;
self.button.layer.shouldRasterize = true;
self.button.layer.rasterizationScale = UIScreen.mainScreen.scale;

对于 Swift 4.2

    self.btnShadow.layer.cornerRadius =  self.btnShadow.frame.size.width / 2
    self.btnShadow.layer.masksToBounds = false
    self.btnShadow.layer.shadowColor = UIColor.black.cgColor
    self.btnShadow.layer.shadowOpacity = 1.0
    self.btnShadow.layer.shadowOffset = CGSize(width: 0, height: 0)
    self.btnShadow.layer.shadowRadius = 10
    self.btnShadow.layer.shouldRasterize = true
    self.btnShadow.layer.rasterizationScale = UIScreen.main.scale

此处的输出为enter image description here,黑色十字为图像