UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 9: Why is CGRect.maxY at the bottom of the rectangle instead of the top?

Forums > 100 Days of SwiftUI

Why is CGRect.maxY at the bottom. of the CGRect instead of the top?

x = 0 is on the left side as I would expect it to be, but y = 0 is at the top which seems very unnatural to me.

For example, this code would define a Shape with a single path going from the top left corner to the bottom right corner, not from the bottom left corner to the top right corner as I would expect.

struct XEqualsY: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()

        path.move(to: CGPoint(x: rect.minX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))

        return path
    }
}

2      

Because NeXT's graphics system was originally based on Display Postscript, which has y0 in the lower left. NeXT begat macOS (even though it no longer uses DPS), so that explains that one. UIKit has it's y0 in the upper left but CoreGraphics still uses lower left. UIKit flips is for you before providing a drawing context.

Sorry, totally misread the question. Let me rephrase...

It's because UIKit helpfully flips the coordinate system before providing you with a drawing context. I assume SwiftUI uses the same underlying logic.

2      

Are you saying "helpfully" sarcistically, or is that actually a helpful feature?

Because it just seems like it makes things a little bit more confusing for me right now. But maybe I just don't understand the wider scope of what that means, or why it is helpful yet.

2      

I personally find it helpful because I find y0 being in the top left more intuitive. It's reminiscent of graphics programs that tend to default the origin point to the top left of the document, so I find it much more useful when laying out UI to picture it the same way. Also, reading in most (all?) languages starts at the top of a page and in English (which is my native tongue) at the left, so that's more natural to me.

And when working with UI components like scroll views or text boxes, top left is more in line with how we think of them as users. (Or, at least, top is; I suppose to people whose native language is RTL, the left part of top left might still seem a bit weird.) Mathematically, it may not be correct for the coordinate system to go from top left to bottom right, but I think in terms of UI, it definitely is better to do it that way.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.