Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want make view this Bottom roundedenter image description here

See Question&Answers more detail:os

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...