提问人:Khashayar 提问时间:9/27/2020 更新时间:7/12/2021 访问量:723
如何在 SwiftUi 中确定 tabBar 的坐标,以便将滑动卡定位在其正上方
How to determine the coordinate of the tabBar in SwiftUi in order to position a slide-over card right above it
问:
我正在使用以下源代码在我的 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
}
}
}
我想以这样一种方式更改底部位置,即卡片始终位于 tabBar 的正上方,就像这样,无论它在哪种屏幕尺寸上使用。
例如,我已经尝试了很多变体,或者仍然没有任何成功。case .bottom: return UIScreen.main.bounds.height - UITabBarController().tabBar.frame.size.height
UIApplication.shared.windows[0].safeAreaLayoutGuide.layoutFrame.maxY - UITabBarController().tabBar.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()
}
}
有没有想到如何获取tabBar正上方的位置坐标?
答:
0赞
Merouane
9/28/2020
#1
我在 SceneDelegate 中使用它来获取底部安全区域大小,您可以将其添加到 TabView 高度,它将为您提供屏幕底部和 TabView 顶部之间的“真实”高度。
let safeAreaInsets = window.safeAreaInsets.bottom
如果有什么不清楚的地方,请告诉我。
干杯
评论
0赞
Khashayar
9/28/2020
使用代码后,例如在 iPhone 11 上,(底部)safeAreaInsets 的值为 34。通过设置卡片仍然远远低于屏幕(-200 的偏移量使其正好位于 tabView 边框上)。因此,不幸的是,问题仍然没有得到解决。case .bottom: UIScreen.main.bounds.height - bottomSafeAreaInsets!
0赞
Merouane
9/28/2020
也减去 TabView 高度?case .bottom: UIScreen.main.bounds.height - bottomSafeAreaInsets!- 80
0赞
Khashayar
9/28/2020
没有帮助。不知何故适用于 iPhone 11。但这不是这个想法。因为在其他 iPhone 中,偏移量不同,并且卡将从希望的位置移位。甚至不匹配。case .bottom: UIScreen.main.bounds.height - bottomSafeAreaInsets! - 200
UIScreen.main.bounds.height - bottomSafeAreaInsets! - UITabBarController().tabBar.frame.size.height
0赞
Luca Rocchi
7/12/2021
#2
获取选项卡栏顶部 y ...然后减去你的视图高度..那是你的 y 坐标......您必须在 viewDidAppear 上执行此操作,在选项卡栏布局完成后
评论