简体   繁体   中英

top right is not working on byRoundingcorners in swift 4

im trying to make rounded corners for the sub view in the top left and top right using the following code

 maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topRight, .topLeft], cornerRadii: CGSize(width: 10, height: 10)).cgPath

also tried in viewDidLayoutSubviews(), but only left border radius is working... the right border radius is not working.

can someone help me out

That often happens when your maskLayer is wider than the view it masks.

See the diagram below. Overlapped area is a rectangle with one corner rounded.

Ë

You may miss configure something , here maskedCorners rounds top left && right --- and roundCorners rounds bottom left && right

class ViewController: UIViewController {

    let redBox = UIView(frame: CGRect(x: 100, y: 100, width: 128, height: 128))
    let blueBox = UIView(frame: CGRect(x: 100, y: 300, width: 128, height: 128))
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        redBox.backgroundColor = .red
        redBox.layer.cornerRadius = 25
        redBox.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        view.addSubview(redBox)

        blueBox.backgroundColor = .blue
        view.addSubview(blueBox)
    }


    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        blueBox.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 25.0)
    }

}

extension UIView {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask
    }
}

在此处输入图片说明

try this

override func layoutSubviews() {
    super.layoutSubviews()

yourView.roundCorners(topLeft: 25, topRight: 25)
bookImage.clipsToBounds = true


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM