切出优秀的观点

时间:2017-09-30 15:16:04

标签: ios swift

我试图创建一个自定义视图,我有一个Button,我希望它与Superview的形状相同。所以我创建了一个带圆角的视图。在那里,我想在视图底部有一个按钮,并且在与超视图相同的位置切出角落。

Idea

我希望这张图片能够理解我的意思。 按钮需要哪些属性与superview具有相同的形状。如果它有帮助,也在这里代码。

let button: UIButton = {
    let button = UIButton()
    button.setTitle("Buy", for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: 22)
    //button.setTitleColor(UIColor.blue, for: .normal)
    button.setTitleColor(UIColor.lightGray, for: .highlighted)
    button.backgroundColor = .gray
    button.clipsToBounds = true
    return button
}()
func show(){

    self.addSubview(background)
    self.addSubview(overview)
    background.frame = self.frame
    background.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(close)))
    overview.frame = CGRect(x: 40, y: 40, width: self.frame.width-80, height: self.frame.height-80)
    let size = overview.frame


    overview.addSubview(button)
    let imageHeight = size.height/3


    button.frame = CGRect(x: 0, y: size.height-38, width: size.width, height: 38)

    UIView.animate(withDuration: 0.5) {
        self.background.alpha = 1
        self.overview.alpha = 1
    }
}

2 个答案:

答案 0 :(得分:2)

尝试在包含该按钮的视图上设置clipsToBounds = true。这应该使它剪辑按钮。

如果不这样做,您可能需要为按钮构建一个遮罩,该遮罩在底角而不是顶部圆角。 (您可以使用UIBezierPath方法init(roundedRect:byRoundingCorners:cornerRadii:)创建一个只有底角倒圆的贝塞尔路径,并将其安装为CAShapeLayer的路径,并将THAT安装为按钮的遮罩层。)

答案 1 :(得分:1)

将“ clipsToBounds ”属性添加到superView(我希望角落的白色背景视图是概览属性),如下所示,

overview.clipsToBounds = true

这将使其子视图显示为角落视图。

相关问题