【问题标题】:Showing Overlay Aligning to the Bottom显示与底部对齐的叠加层
【发布时间】:2021-12-31 09:54:08
【问题描述】:

当我点击一个按钮时,我想将View 放在另一个上面。以下是我的代码。

import SwiftUI

struct SheetView: View {
    @State private var showSheet: Bool = false
    var body: some View {
        NavigationView {
            VStack {
                ZStack {
                    Rectangle()
                        .fill(Color.orange)
                        .frame(height: 32.0)
                    Button("Please select a mailing address") {
                        showSheet.toggle()
                    }.foregroundColor(Color.black)
                }
                Spacer()
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationViewStyle(StackNavigationViewStyle())
        }
        .overlay(popOver)
    }
    
    var popOver: some View {
        Group {
            if showSheet {
                ZStack {
                    Color.black.opacity(0.4).ignoresSafeArea()
                    ZStack {
                        Rectangle()
                            .fill(Color.white)
                            //.frame(width: UIScreen.main.bounds.width, height: 400)
                            .frame(maxWidth: .infinity, maxHeight: 320.0, alignment: .bottom)
                            //.position(x: UIScreen.main.bounds.width / 2.0, y: 600)
                    }
                }.onTapGesture {
                    showSheet.toggle()
                }
            }
        }
    }
}

它看起来像the following picture。除了覆盖的View 将出现在中心之外,我几乎得到了我需要的东西。我怎样才能让它出现,对齐底部View?谢谢。

【问题讨论】:

    标签: ios swiftui overlay


    【解决方案1】:

    这是一个修复 - 使用内部 ZStack 对齐(使用 Xcode 13.2 / iOS 15.2 测试):

        ZStack(alignment: .bottom) {
            Color.black.opacity(0.4).ignoresSafeArea()
            ZStack {
                Rectangle()
                    .fill(Color.white)
                    .frame(maxWidth: .infinity, maxHeight: 320.0, alignment: .bottom)
            }
        }
        //.ignoresSafeArea()     // << probably you also need this
        .onTapGesture {
            showSheet.toggle()
        }
    

    【讨论】:

    • 谢谢。不过,在我进行更改后,叠加层仍会出现在中心。
    • 看到更新了,没认出你用的是内部ZStack
    • ZStack(alignment: .bottom) 做到了。谢谢。
    猜你喜欢
    • 2019-04-10
    • 2020-12-28
    • 2015-10-16
    • 1970-01-01
    • 2021-11-16
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多