Where do this "rect" variable come from?

Viewed 39

I copied this period of code to make a clipped shape of rounded corner. But the "rect" variable make me puzzled. It isn't an input variable.How can I use this struct without passing a value or initialize it.

import SwiftUI

struct RoundedShape:Shape{
    var corners:UIRectCorner
    func path(in rect:CGRect) -> Path {
        let path=UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: 80, height: 80))
        return Path(path.cgPath)
        
    }
}

⬆️ This is what I got from Internet

   Rectangle()
            .clipShape( RoundedShape(corners: [.bottomRight]))
       
    }

And I use this struct here and got this

image

Nothing wrong with the result,I just don't understand why this works without any passing or initializing to the "rect".

If you can help me, I appreciate it a lot.

1 Answers

A shape takes all available space in a place where it is created. So rect is injected by SwiftUI layout engine and in your case it is all space consumed by parent Rectangle:

   Rectangle()   // << bounds of this view
            .clipShape( RoundedShape(corners: [.bottomRight]))
Related