右上角无法在byRoundingcorners中快速运行4

时间:2018-11-29 15:00:37

标签: ios swift uiview

im尝试使用以下代码为左上角和右上角的子视图制作圆角

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

也在viewDidLayoutSubviews()中尝试过,但是只有左边界半径有效...右边界半径无效。

有人可以帮我吗

3 个答案:

答案 0 :(得分:0)

这通常发生在您的maskLayer宽于其所遮盖的视图时。

请参见下图。重叠的区域是一个矩形,其一个角是圆的。

e

答案 1 :(得分:0)

您可能会错过一些配置,这里maskedCorners左上角&&右---和roundCorners左下角&&右

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
    }
}

enter image description here

答案 2 :(得分:0)

尝试

override func layoutSubviews() {
    super.layoutSubviews()

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


}