原始想法是同时设置maskToBounds = true 和 cornerRadius,然而并不能看到阴影,因为阴影已经被mask掉了
大家的做法都是加一个层或者加一个view
如下
然而 只有当把下面的addButton添加到shadow这个view里面的时候才会正确显示阴影,如果把addButton和shadow添加到另外某一个view则无法正确显示,原因待查证。
效果如图
Paste_Image.png
let addButtonRadius:CGFloat = 28
let shadow = UIView()
shadow.layer.shadowColor = AtColor.blue2().CGColor
shadow.layer.shadowRadius = 5
shadow.layer.shadowOffset = CGSize(width:0, height:0)
shadow.layer.shadowOpacity = 1
shadow.layer.cornerRadius = addButtonRadius
shadow.clipsToBounds = false
self.view.addSubview(shadow)
shadow.snp.makeConstraints { (btn) in
btn.right.equalToSuperview().offset(-34)
btn.bottom.equalToSuperview().offset(-56)
btn.width.height.equalTo(addButtonRadius * 2)
}
addButton = UIButton()
addButton.titleEdgeInsets = UIEdgeInsets(top: 3, left: 0, bottom: -3, right: 0)
addButton.layer.masksToBounds = true
addButton.layer.cornerRadius = addButtonRadius
addButton.setBackgroundImage(AtColor.blue1().getImage(), for: UIControlState.normal)
addButton.setBackgroundImage(AtColor.blue2().getImage(), for: UIControlState.highlighted)
addButton.titleLabel?.font = AtFontIcon.getFont(20)
addButton.setTitle(FontIconType.HomePlus.rawValue, for: UIControlState.normal)
addButton.setTitleColor(AtColor.white().color, for: UIControlState.normal)
shadow.addSubview(addButton)
addButton.snp.makeConstraints { (btn) in
btn.edges.equalToSuperview()
}
addButton.addTarget(self, action: #selector(self.onAddGroupButtonClick), for: UIControlEvents.touchUpInside)