原始想法是同時(shí)設(shè)置maskToBounds = true 和 cornerRadius饲宛,然而并不能看到陰影艇抠,因?yàn)殛幱耙呀?jīng)被mask掉了
大家的做法都是加一個(gè)層或者加一個(gè)view
如下
然而 只有當(dāng)把下面的addButton添加到shadow這個(gè)view里面的時(shí)候才會(huì)正確顯示陰影久锥,如果把a(bǔ)ddButton和shadow添加到另外某一個(gè)view則無法正確顯示,原因待查證媒鼓。
效果如圖
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)