Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a Rectangle View Using the Diagonal in Swift UI

Tags:

ios

swiftui

I'm new to SwiftUI and I'm trying to achieve something that is quite easy using UIKit. Basically, I want to draw something like this in a View:

enter image description here

I figured that I could use GeometryScanner but I didn't manage to make it work. Any help would be appreciated.

like image 679
iDev Avatar asked Apr 09 '26 00:04

iDev


1 Answers

You can create your own Triangle:

struct Triangle: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: rect.maxX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
        return path
    }
}

and use it like this:

Rectangle()
    .fill(Color.white)
    .frame(width: 300, height: 200)
    .overlay(
        Triangle()
            .fill(Color.red)
    )
like image 183
pawello2222 Avatar answered Apr 12 '26 06:04

pawello2222



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!