带有阴影和圆角的UIView-Swift

时间:2020-04-20 14:59:29

标签: ios swift uitableview uiview rounded-corners

我在视图中有一个表格视图单元格。我希望该视图具有阴影和圆角。因此,我创建了一个类:

class UIViewWithShadowAndRoundedCorner: UIView {
    @IBOutlet var roundedView: UIView!

    override func layoutSubviews() {
        super.layoutSubviews()
        self.addShadow()
    }

    public func addShadow() {
        self.roundedView.layer.cornerRadius = 20
        self.roundedView.layer.masksToBounds = true

        self.layer.shadowColor = UIColor.gray.cgColor
        self.layer.shadowRadius = 1
        self.layer.shadowOffset = .zero
        self.layer.shadowOpacity = 1
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.addShadow()
    }
}

表格视图单元格中的视图是UIViewWithShadowAndRoundedCorner,但我得到了这个结果(视图没有圆角,只有阴影):

shadow but no rounded corners

如果我创建一个仅包含一个UIViewController的项目,并且在其中包含一个UIViewWithShadowAndRoundedCorner的实例,我会得到这个(我想要得到的结果):

desired result shadow + rounded corner view

为什么使用相同的视图类型会得到不同的结果?

0 个答案:

没有答案