gpt4 book ai didi

swiftui - 如何在父 View 使用 IgnoringSafeArea() 时访问 safeArea 插图

转载 作者:行者123 更新时间:2023-12-05 06:11:14 29 4
gpt4 key购买 nike

如您所见,我想将搜索栏恰好放在 safeArea 的顶部,但是 proxy.safeAreaInsets 没有正确的值,因为在PreviewProvider 父级使用 edgesIgnoringSafeArea

我能做什么?有什么方法可以访问 safeAreaInsets 吗?

struct FindView: View {

// MARK: - Properties
@ObservedObject var viewModel: FindViewModel

init(viewModel: FindViewModel){
self.viewModel = viewModel

}

var body: some View {

GeometryReader { proxy in
ScrollView(.vertical, showsIndicators: true, content: {
VStack(alignment: .leading, spacing: 8){
SearchBar()
.frame(height: 48, alignment: .center)
.padding(.all, 16)


Text("Categories")
.multilineTextAlignment(.leading)
.font(.system(size: 21))
.padding(.all, 16)

}.frame(width: proxy.size.width)
})
}
}
}

struct FindView_Previews: PreviewProvider {
static var previews: some View {
ZStack(alignment: .center, content: {
Rectangle().foregroundColor(.red)
FindView(viewModel: FindViewModel())
}).edgesIgnoringSafeArea(.all)
}
}

Preview of the code

最佳答案

简单的方法;

     struct test: View {
var body: some View {
VStack{
Text("Your view comes here")
Spacer()
}
.frame(minWidth:0, maxWidth: .infinity, minHeight: 0,maxHeight: .infinity, alignment: .center)
.padding(.top,UIApplication.shared.windows.first?.safeAreaInsets.top ?? 40)
.background(Color.red)
.edgesIgnoringSafeArea(.all)
}
}

关于swiftui - 如何在父 View 使用 IgnoringSafeArea() 时访问 safeArea 插图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64079431/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com