1

我正在尝试使圆圈适合大小不会增加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")
                }
            }
        }
    }
}

界

4

1 回答 1

1

这是可供选择的各种容器的示例。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)
                }
            }
        }
    }
}

圈子视图

于 2021-05-10T02:20:42.400 回答