在 SwiftUI 中更改 ProgressView 的大小(高度)

Mic*_*al1 7 xcode uiprogressview ios swift swiftui

ProgressView在 SwiftUI 中创建了一个(使用 Xcode)并进行了一些编辑,但还没有弄清楚如何改变它的高度。

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))
    }
}
Run Code Online (Sandbox Code Playgroud)

代码截图

ahe*_*eze 13

我知道没有直接的方法来改变高度,但你可以使用.scaleEffect修饰符。确保指定1x 比例以仅增加高度。

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果: 具有较大高度的进度条

这样做的一个缺点是您不能传入 Label,因为它也会被拉伸。

ProgressView("Progress:", value: 50, total: 100)
Run Code Online (Sandbox Code Playgroud)

标签“进度:”垂直拉伸

要解决此问题,只需TextProgressView.

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

“进度:”未拉伸

  • 它可以工作,但是您可以在 ProgressView 中创建圆角吗?如何? (4认同)
  • @CGR 很难自定义来自系统的诸如 ProgressView 之类的东西。[制作自己的进度条](https://www.simpleswiftguide.com/how-to-build-linear-progress-bar-in-swiftui/)可能会更容易。 (3认同)

Aly*_*kan 5

我需要一些半径更圆的东西,并且不将 ProgressView 隐藏在另一个视图中,所以这是我的看法(通过使用缩放效果扩展 @aheze 的答案)

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))
Run Code Online (Sandbox Code Playgroud)