Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

swift - Create a UIView with rounded bottom edge

I want make view this Bottom roundedenter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is really close to what you want:

The rectView is your initial view; You can play with the + 50 of quad curve height to adjust the curve

let rectView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 200))
    rectView.backgroundColor = .red
    view.addSubview(rectView)

    let arcBezierPath = UIBezierPath()
    arcBezierPath.move(to: CGPoint(x: 0, y: rectView.frame.height))
    arcBezierPath.addQuadCurve(to: CGPoint(x: rectView.frame.width, y: rectView.frame.height), controlPoint: CGPoint(x: rectView.frame.width / 2 , y: rectView.frame.height + 50 ))
    arcBezierPath.close()

    let shapeLayer = CAShapeLayer()

    shapeLayer.path = arcBezierPath.cgPath

    shapeLayer.fillColor = UIColor.red.cgColor

    rectView.layer.insertSublayer(shapeLayer, at: 0)
    rectView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    rectView.layer.cornerRadius = 10
    rectView.layer.shadowRadius = 3
    rectView.layer.shadowColor = UIColor.black.cgColor
    rectView.layer.shadowOffset = CGSize(width: 0, height: 0)
    rectView.layer.shadowOpacity = 0.25

Maybe, even better: You can create your view:

let rectView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 200))
view.addSubview(rectView)

Extend UIView

extension UIView {
    func addBottomArc(ofHeight height: CGFloat, topCornerRadius: CGFloat) {
       let arcBezierPath = UIBezierPath()
       arcBezierPath.move(to: CGPoint(x: 0, y: frame.height))
       arcBezierPath.addQuadCurve(to: CGPoint(x: frame.width, y: frame.height), controlPoint: CGPoint(x: frame.width / 2 , y: frame.height + height ))
       arcBezierPath.close()

       let shapeLayer = CAShapeLayer()

       shapeLayer.path = arcBezierPath.cgPath
       shapeLayer.fillColor = UIColor.red.cgColor

       layer.insertSublayer(shapeLayer, at: 0)
       layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
       layer.cornerRadius = topCornerRadius
    }
}

So you can set the height and the corner radius like this:

rectView.addBottomArc(ofHeight: 50, topCornerRadius: 15)

and than add the shadow you need to your view:

    rectView.layer.shadowRadius = 3
    rectView.layer.shadowColor = UIColor.black.cgColor
    rectView.layer.shadowOffset = CGSize(width: 0, height: 0)
    rectView.layer.shadowOpacity = 0.25

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...