SwiftUI Shape Scale Size 使得 HStack 大小不会增加

问题描述

我正在尝试使圆圈适合 HStack,这样 HStack 的大小就不会增加。 如何在不指定固定框架的情况下使圆圈适合?

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            Form {
                HStack {
                    Circle()
                        .fill(Color.red)
                        .aspectRatio(1,contentMode: .fit)
                    Text("Hello")
                }
                HStack {
                    Circle()
                        .fill(Color.blue)
                        .aspectRatio(1,contentMode: .fit)
                    Text("Hello")
                }
            }
        }
    }
}

Circles

解决方法

以下是可供选择的各种容器的示例。 SwiftUI 将完成所有布局,自动处理旋转和设备分辨率。

struct CirclesView: View {
    var body: some View {
        VStack(spacing: 0) {
            Label("Circles",systemImage: "circle").font(.system(size: 24,weight: .black,design: .rounded)).foregroundColor(.pink)
            HStack {
                Circle()
                    .foregroundColor(.yellow)
                    .frame(width: 32,height: 32)
                Text("This is a yellow circle")
                Spacer()
            }
            Circle()
                .foregroundColor(.orange)
                .shadow(radius: 10)
                .frame(width: 75)
            Divider()
            HStack {
                VStack {
                    Circle().foregroundColor(.blue)
                    Text("Blue").font(.title3)
                    HStack {
                        Circle().foregroundColor(.purple)
                        Text("Purple").font(.caption)
                    }
                }
                .padding()
                .background(Color.yellow)
                ZStack(alignment: Alignment(horizontal: .center,vertical: .center)) {
                    Circle().foregroundColor(.green)
                    Text("Green").foregroundColor(.primary)
                }
            }
        }
    }
}

Circles View