Ios 如何在SwiftUi中确定选项卡栏的坐标,以便将滑盖卡定位在其正上方

Ios 如何在SwiftUi中确定选项卡栏的坐标,以便将滑盖卡定位在其正上方,ios,swift,iphone,xcode,swiftui,Ios,Swift,Iphone,Xcode,Swiftui,我正在使用用于在我的SwiftUi应用程序中构建滑盖卡。卡位置由以下代码部分定义: public enum CardPosition: CGFloat { case bottom , middle, top func offsetFromTop() -> CGFloat { switch self { case .bottom: return UIScreen.main.bounds.height

我正在使用用于在我的SwiftUi应用程序中构建滑盖卡。卡位置由以下代码部分定义:

public enum CardPosition: CGFloat {
    
    case bottom , middle, top
    
    func offsetFromTop() -> CGFloat {
        switch self {
        case .bottom:
            return UIScreen.main.bounds.height - 80
        case .middle:
            return UIScreen.main.bounds.height/1.8
        case .top:
            return 80
        }
    }
}
我想改变底部的位置,使卡片始终位于选项卡栏的正上方,无论使用的屏幕大小如何。 我尝试了许多变体,例如
case.bottom:return UIScreen.main.bounds.height-uitabbar.frame.size.height
UIApplication.shared.windows[0].saferealayoutguide.layoutFrame.maxY-uitabbar.frame.size.height,但仍然没有成功

我使用的代码模板如下所示:

struct ContentView: View {

    // for SlideOverCard
    @State private var position = CardPosition.bottom
    @State private var background = BackgroundStyle.blur
    
    var body: some View {
        TabView {
            NavigationView {
                ZStack(alignment: Alignment.top){
                    List() { Text("Elements") }
                    
                    SlideOverCard($position, backgroundStyle: $background) {
                        Form{Text("Some content")}
                    }
                    
                }
                .navigationBarTitle(Text("List"))
            }
            .tabItem {
                Image(systemName: "person.crop.circle")
                Text("Tab")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

你知道如何获得选项卡栏正上方的位置坐标吗?

我在SceneDelegate中使用它来获得底部安全区域的大小,你可以将其添加到选项卡高度,它将为你提供屏幕底部和选项卡顶部之间的“真实”高度

let safeAreaInsets = window.safeAreaInsets.bottom
如果有不清楚的地方,请告诉我


干杯

使用您的代码后,例如在iPhone 11上,(底部)安全区域插图的值为34。通过设置
case.bottom:UIScreen.main.bounds.height-bottomSafeAreaInsets该卡仍远低于屏幕(偏移量为-200使其正好位于选项卡边框上)。因此,不幸的是,这个问题仍然没有解决。减去TabView的高度也是吗?case.bottom:UIScreen.main.bounds.height-底部安全区域插图!-没有帮助。不知何故
case.bottom:UIScreen.main.bounds.height-bottomsafereainsets!-200将适用于iPhone 11。但这不是我们的想法。因为在其他iPhone中,偏移量不同,卡会从所需位置移动。偶数
UIScreen.main.bounds.height-bottomSafeAreaInsets!-UITabBarController().tabBar.frame.size.height
不匹配。