【发布时间】:2020-07-14 05:44:21
【问题描述】:
您好,我是 SwiftUI 新手,遇到了问题。
我想制作一个底栏(可重用模块)以在多个视图中使用。
但由于其兄弟视图,它在每个视图中的位置都不同
这些是我尝试过的:
- 几何阅读器 但是每个 View 的几何形状都不同。
- 叠加和位置(x:y:) 这也不起作用
- ZStack
我的问题是
- 如何制作固定位置视图? (如位置:css 中的绝对值)
- 此问题的正常解决方案是什么?
- 是否有任何对 SwiftUI 有用的参考资料?
我想将此模块用作底部按钮栏
struct BaseBottomButton: View {
let screen = UIScreen.main.bounds
var body: some View {
Button(action: {
print("hello")
}) {
Text("Hello")
}
.frame(width: self.screen.width ,height: 40)
.background(Color.red)
}
}
在这个视图中,它有效
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
VStack {
VStack {
Text("What")
.frame(width: 400, height: 50)
.background(Color.green)
}
Spacer()
Group {
VStack {
Text("Some Text")
Text("")
.frame(width: 200, height: 200)
.background(Color.yellow)
}
}
Spacer()
BaseBottomButton()
}
}
}
}
但在这个复杂的视图中,它不起作用
struct NewView: View {
var body: some View {
GeometryReader { geometry in
ScrollView(showsIndicators: false) {
VStack(spacing: 0) {
Text("hello")
.frame(width: 200, height: 200)
.background(Color.blue)
ZStack(alignment: .leading) {
VStack {
Spacer()
.frame(height: 24)
}
.padding(.leading, 20)
.padding(.bottom, 138)
Text("some box")
.frame(width: 200, height: 100)
.background(Color.yellow)
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.gray)
Spacer(minLength: 40)
}
BaseBottomButton()
}
}
}
}
【问题讨论】:
-
你会添加你的代码吗?
-
@Asperi 我添加了我的示例代码和图像
标签: swiftui