【问题标题】:Filling HStack swiftUI填充 HStack swiftUI
【发布时间】:2021-04-24 00:47:17
【问题描述】:

我想创建一个按钮堆栈,就像使用 SwiftUI 有一个播放和录制按钮一样,创建后它看起来不像我想要的那样。

var body: some View {
        
        HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: 8) {
            Spacer()
            Button(action: {
                print("Recordinggg")
            }, label: {
                Text("Record")
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(40.0)
            })
            Spacer()
            Button(action: {
                print("Recordinggg")
            }, label: {
                Text("Play")
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(40.0)
            })
            Spacer()
        }
        
        
    }

我真正想要的是这样的

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    使用适当的框架,填充可以实现这一点。这是一个示例代码。

    创建按钮样式。

    struct ThemeButtonStyle: ButtonStyle {
        func makeBody(configuration: Self.Configuration) -> some View {
            configuration.label
                .padding([.top, .bottom], 10)
                .foregroundColor(.white)
                .frame(minWidth: 0, maxWidth: .infinity, alignment: .center)
                .background(Color.blue)
                .cornerRadius(40.0)
        }
    }
    

    你的看法

    struct ContentView: View {
      @State private var phase: CGFloat = 0
        var body: some View {
            
            HStack(alignment: .center, spacing: 0) {
                Button(action: {
                    print("Recordinggg")
                }, label: {
                    Text("Record")
                })
                .buttonStyle(ThemeButtonStyle())
                
                Spacer()
                Button(action: {
                    print("Recordinggg")
                }, label: {
                    Text("Play")
                })
                .buttonStyle(ThemeButtonStyle())
            }
            .padding()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 2020-03-12
      • 1970-01-01
      • 2021-04-12
      • 2022-04-11
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多