11

how can i pass LinearGradient to a shape (for example Rectangle) just in SwiftUI?

Rectangle().frame(width: UIScreen.main.bounds.width, height: 200)
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Sajad Beheshti
  • 679
  • 3
  • 8
  • 20
  • 1
    [`Rectangle.fill`](https://developer.apple.com/documentation/swiftui/rectangle/3286692-fill) and [`Rectangle.fill`](https://developer.apple.com/documentation/swiftui/rectangle/3286693-fill) (yes, they are slightly different, so you will need to look at them and decide which meets your needs) – MadProgrammer Jun 07 '19 at 05:50
  • @MadProgrammer Thank, right before you answer, i found that the fill method should be called as the first thing which is added and i was trying to use that as second parameter. – Sajad Beheshti Jun 07 '19 at 06:02
  • @MadProgrammer Both Rectangle.fill links are broken. – drewster Dec 02 '19 at 20:17
  • @drewster Good thing it's just a comment - you could search just "SwiftUI rectangle" – MadProgrammer Dec 02 '19 at 20:43

4 Answers4

28

This should work:

static let gradientStart = Color(red: 239.0 / 255, green: 120.0 / 255, blue: 221.0 / 255)
static let gradientEnd = Color(red: 239.0 / 255, green: 172.0 / 255, blue: 120.0 / 255)

var body: some View {
  Rectangle()
    .fill(LinearGradient(
      gradient: .init(colors: [Self.gradientStart, Self.gradientEnd]),
      startPoint: .init(x: 0.5, y: 0),
      endPoint: .init(x: 0.5, y: 0.6)
    ))
    .frame(width: 300, height: 200)
}
M Reza
  • 18,350
  • 14
  • 66
  • 71
  • could u explain what the x & y in ```startPoint``` and ```endPoint``` means? It just has a slightly different with 0 -> 0.6, what position it is rely on. – Zhou Haibo Mar 02 '20 at 04:50
7

SwiftUI

Here is a possible solution.

struct TestSwiftUIView: View {
    let uiscreen = UIScreen.main.bounds

    var body: some View {
        Rectangle()
        .fill(
            LinearGradient(gradient: Gradient(colors: [Color.clear, Color.black]),
                           startPoint: .top,
                           endPoint: .bottom))
        .frame(width: self.uiscreen.width,
               height: self.uiscreen.height,
               alignment: .center)
    }
}

This code snippet will produce a screen like that:

Showing a screen with a gradient from white to black

The startPoint and the endPoint are UnitPoint. For UnitPoints you have the following options:

.zero

.center

.leading

.trailing

.top

.bottom

.topLeading

.topTrailing

.bottomLeading

.bottomTrailing
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
2

As of beta 6, you must set the forgroundColor to clear first.

Rectangle()
    .foregroundColor(.clear)
    .background(LinearGradient(gradient:  Gradient(colors: [Color("gradient1"), Color("gradient2")]), startPoint: .top, endPoint: .bottom))
2
Rectangle().frame(width: UIScreen.main.bounds.width, height: 200) // You original code

.overlay(
    LinearGradient(gradient: Gradient(colors: [.red, .purple]), startPoint: .top, endPoint: .bottom))
)

Note that:

When you apply an overlay to a view, the original view continues to provide the layout characteristics for the resulting view.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278