6

I am following Apples "Creating and Combining View" SwiftUI Tutorial and on Section 4 Step 5 I am asked to add a Gray Stroke, which I can do but doesn't show the colour or give me a line width.

            .clipShape(Circle())
            .overlay(
                Circle().stroke(StrokeStyle)) 

Am I doing something wrong or is this some kind of bug?

As Apples example shows;

           .clipShape(Circle())
           .overlay(
               Circle().stroke(Color.gray, lineWidth: 4))

I can type it in but don't understand why it is not pulling the colour from the Inspector. I can even amend the colour from Gray to White but again I cannot see this referenced in the code

Also is it possible to add/set a lineWidth from the Inspector? The next step with adding the shadow made perfect sense but this step has really stumped me.

SD449
  • 97
  • 1
  • 7

3 Answers3

4

The stroke(...) function of Circle() is overloaded multiple times.

function overloading or method overloading is the ability to create multiple functions of the same name with different implementations.

You can read about function overloading here: Function overloading

Heres the Documentation of the four different implementations of stroke(...)

stroke(style:)

stroke(lineWidth:)

stroke(_:style:)

stroke(_:lineWidth:)

all of them are valid and you can decide which one you use or which one best suits your needs.

Circle().stroke(Color.black, lineWidth: 1)  
Circle().stroke(style: StrokeStyle(lineWidth: 1, lineCap: .butt, lineJoin: .bevel, miterLimit: 1, dash: [CGFloat](), dashPhase: 1))

Both of this implementations do the same but are using different implementations of the stroke(...) function.

KevinP
  • 2,562
  • 1
  • 14
  • 27
1

If you mean color literals, then they are UIColor type, so you have to wrap them explicitly in Color constructor, like below

demo

Asperi
  • 228,894
  • 20
  • 464
  • 690
0

You can do this:

Circle()
.stroke(.gray.opacity(0.2),lineWidth: 4)