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

Noob question: linear gradients

Forums > SwiftUI

This feels like a pretty innapropraite question considering I'm very new to SwiftUI and this forum is for advanced users, but I've been fiddling around and can't find any explaination. I used a linear gradient as a background for a rectangle behind a series of buttons in a ZStack, and the gradient is very transparent, almost unnoticable, i've tried chaing the opacity to no avail. I suspect this must be a beginner mistake and I am just too unfamiliar to even understand the realm of this issue, but I'd appreciate any help. Here is an image: ! (The original colors were a solid, vibrant orange and red.) Also, the background behind both the buttons and the rectangle is white. Here is the code for the gradient: Rectangle() .frame(height: 20.0) .background(LinearGradient(gradient: Gradient(colors: [Color("Orange"), Color("Red")]), startPoint: .leading, endPoint: .trailing))

Thanks!

3      

Hi Conrad. I think you need to change Color("Orange") and Color("Red") to Color.orange and Color.red (or even just .orange and .red).

4      

Hi Conrad

I think in this forum there are a lot of guys learning stuff. Everybody has to start somewhere.

What Nigel suggests is true and probably what you are looking for: You want to use a predefined color provided by Apple. Therefore .orange and .red (as a shortcut for Color.orange and Color.red).

But if you want to use a custom color which you have defined in the Assets.xcassets as a "Color Set", you have to use the String-based initializer as you did. If you check the documentation for Color you see that this initializer takes a Bundle parameter, therefore indicating the source location for this externally defined color.

Further, the issue with your Rectangle() example is, that it also has a fill color (as you didn't specify any it uses black) which covers completely your background gradient you specified. So to make it visible I suggest to stroke the Rectangle to make the background visible:

// Not working gradient: "Orange" and "Red" are not defined
Rectangle()
    .stroke(Color.gray)
    .frame(height: 20.0)
    .background(LinearGradient(gradient: Gradient(colors: [Color("Orange"), Color("Red")]), startPoint: .leading, endPoint: .trailing))

// Working gradient: .orange and .red defined by SwiftUI
Rectangle()
    .stroke(Color.gray)
    .frame(height: 60.0)
    .background(LinearGradient(gradient:
        Gradient(colors: [.orange, .red]),
                               startPoint: .leading,
                               endPoint: .trailing))

I hope this helps you understand the issue.

regards Philipp

4      

Hi Conrad,

I agreed with Philipp that a lot of people are learning with Hacking with Swift and Paul Hudson so this is a great place to ask your question as it has a code of conduct. The other thing you can also try when looking for answer is google it and I think you find that developers do this also. But some time it hard to find correct answer.

With regard to your question you might want to check out Paul video here

https://www.hackingwithswift.com/books/ios-swiftui/gradients

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

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.