24

I created a ProgressView in SwiftUI (using Xcode) and edited a bit but haven’t figured out how to change its height.

struct ProgressBar: View {
    var body: some View {
        VStack {
            ProgressView("Progres:", value: 50, total: 100)
        }.foregroundColor(Color(UIColor.systemBlue))
        .scaleEffect(1, anchor: .center)
        .accentColor(Color(UIColor.systemGreen))
    }
}
shim
  • 9,289
  • 12
  • 69
  • 108
Michal1
  • 357
  • 1
  • 3
  • 8

2 Answers2

34

There's no direct way that I know of to change the height, but you can use the .scaleEffect modifier. Make sure to specify 1 for the x scale in order to only increase the height.

struct ContentView: View {
    var body: some View {
        ProgressBar()
        .padding([.leading, .trailing], 10)
    }
}

struct ProgressBar: View {
    var body: some View {
        VStack {
            ProgressView(value: 50, total: 100)
            .accentColor(Color.green)
            .scaleEffect(x: 1, y: 4, anchor: .center)
        }
    }
}

Result: Progress bar with larger height

A drawback to this is that you can't pass in a Label, because it will also get stretched.

ProgressView("Progress:", value: 50, total: 100)

The label "Progress:" stretched vertically

To work around this, just make your own Text above the ProgressView.

struct ProgressBar: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Progress:")
            .foregroundColor(Color.blue)
            
            ProgressView(value: 50, total: 100)
            .accentColor(Color.green)
            .scaleEffect(x: 1, y: 4, anchor: .center)
        }
    }
}

"Progress:" is not stretched

aheze
  • 24,434
  • 8
  • 68
  • 125
  • 4
    It works but Can you create Rounded Corners in that ProgressView? How? – CGR Jul 16 '21 at 07:35
  • 3
    @CGR it's hard to customize stuff like ProgressView that are from the system. It might be easier to just [make your own progress bar](https://www.simpleswiftguide.com/how-to-build-linear-progress-bar-in-swiftui/). – aheze Jul 16 '21 at 20:05
  • It doesn't work for me on macOS Ventura and swift 5.2. – Nate Lockwood Apr 25 '23 at 22:28
14

I needed something with a more rounded radius and without concealing the ProgressView inside another View so here's my take on it (extending @aheze 's answer by using the scale effect)

struct MyProgressViewStyle: ProgressViewStyle {
  var myColor: Color

  func makeBody(configuration: Configuration) -> some View {
      ProgressView(configuration)
          .accentColor(myColor)
          .frame(height: 8.0) // Manipulate height, y scale effect and corner radius to achieve your needed results
          .scaleEffect(x: 1, y: 2, anchor: .center)
          .clipShape(RoundedRectangle(cornerRadius: 6))
          .padding(.horizontal)
  }
}
// Here's how to use it inside a View
ProgressView(value: currentProgress, total: totalProgress)
            .progressViewStyle(MyProgressViewStyle(myColor: Color.green))
Aly Yakan
  • 471
  • 5
  • 8