NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: How to use a number to set UnitPoint for Color Gradient (not a Type property like ".leading")

Forums > SwiftUI

I'm trying to understand UnitPoint so I can set the startPoint and endPoint of a color linear Gradient with a number, like a CGFloat or a percent instead of the Property Type options I find in the documentation (.leading, .trailing, etc.). Note the code below builds for me:

                .fill(LinearGradient(gradient: Gradient(colors: [color1, color2]), startPoint: .top, endPoint: .bottom))

BUT I want to replace .top with an x, y coordinate, for example, like: startPoint: (x: 0,y: 0) I've tried percents, CGFloats, etc.

BTW, if anyone knows of a great tutorial or class to learn how to read the Documentation pages please share (gosh darn how I wish they always included sample code on these pages).


UnitPoint(x: 0, y: 0) is .topLeading

UnitPoint(x: 1, y: 1) is .bottomTrailing

Use values between 0 and 1 to indicate points between those two corners.


Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can 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!

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.