SwiftUi左下方按钮不显示

时间:2019-12-29 10:38:50

标签: swift button swiftui

我有一些SwiftUI函数。我想在左下方显示我的按钮,但在左上方显示。

struct NewRecordButton: View {

    var body: some View {

        ZStack (alignment: .bottomTrailing) {
            HStack (alignment: .bottom) {
                Spacer()
                Button(action:  { },
                       label: {
                           Text("⌂")
                               .font(.system(.largeTitle))
                               .frame(width: 35, height: 30)
                               .foregroundColor(Color.white)
                               .padding(.bottom,4)
                })  .background(Color.black)
                    .cornerRadius(10)
                    .shadow(color: Color.black.opacity(0.3),
                            radius: 3,
                            x: 3,
                            y: 3)
            }
        }
    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这是可能的变体

ZStack (alignment: .bottomTrailing) {
    Rectangle().fill(Color.clear)
    HStack (alignment: .bottom){
        Button(action:  {

        }, label: {
            Text("⌂")
                .font(.system(.largeTitle))
                .frame(width: 35, height: 30)
                .foregroundColor(Color.white)
                .padding(.bottom,4)


        })
            .background(Color.black)
            .cornerRadius(10)
            .shadow(color: Color.black.opacity(0.3),
                    radius: 3,
                    x: 3,
                    y: 3)

        Spacer()
    }
}

答案 1 :(得分:1)

尽管有可能,但是,您应该尝试使用虚拟视图来排列其他视图!

要下推视图,应同时使用VStackSpacer

VStack { 
    Spacer()
    // bottomView
}

所以应该像这样:

ZStack {
    VStack {
        Spacer() // This will push it down, since it is in a `VStack`
        HStack {

            Button("⌂") {}
                .font(.largeTitle)
                .frame(width: 35, height: 30)
                .foregroundColor(.white)
                .padding(.bottom, 4)

                .background(Color.black)
                .cornerRadius(10)
                .shadow(color: Color.black.opacity(0.3), radius: 3, x: 3, y: 3)

            Spacer() // This will push it left, since it is in a `HStack`
        }
    }
}