问题描述
我正在尝试制作一个将 Blur() 放在 iPhone 布局底部的视图,尊重安全区域并且我可以轻松重用.
I'm trying to make a View that will put a Blur() at the bottom of an iPhone layout, respecting safeareas and that I can easily reuse.
像这样:
import SwiftUI
struct SafeBottomBlurContainer: View {
@Environment(\.colorScheme) var colorScheme
var body: some View {
GeometryReader { geometry in
VStack {
Spacer()
ZStack {
Blur(style: self.colorScheme == .dark ? .systemThinMaterialDark : .systemThinMaterialLight)
.frame(
width: geometry.size.width,
height: geometry.safeAreaInsets.bottom + 50
)
Holder()
.padding(.bottom, geometry.safeAreaInsets.bottom)
}
}
.edgesIgnoringSafeArea(.bottom)
}
}
}
struct Holder: View {
var body: some View {
Rectangle().opacity(0.5)
.frame(height: 50)
}
}
struct SafeBottomBlurContainer_Previews: PreviewProvider {
static var previews: some View {
SafeBottomBlurContainer()
}
}
顺便说一下,这是~~blue~~模糊扩展:
Here is the ~~blue~~ blur extension, by the way:
import SwiftUI
struct Blur: UIViewRepresentable {
var style: UIBlurEffect.Style = .systemMaterial
func makeUIView(context: Context) -> UIVisualEffectView {
return UIVisualEffectView(effect: UIBlurEffect(style: style))
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.effect = UIBlurEffect(style: style)
}
}
现在,我想做的是以某种方式传入 Holder()
以便我可以调整模糊的高度(这里是 + 50).我觉得我应该以某种方式使用 AnyView,但无法弄清楚.那可能是我走错了路.
Now, what I'd like to do is somehow pass in Holder()
so that I can adjust the height of the Blur (which here is + 50). I feel like I should be using AnyView in some manner, but can't figure it out. That might be me on the wrong track.
这是我喜欢在 ContentView() 示例中使用它的方式:
Here is how I'd like to use it in a ContentView() example:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
Color.pink.edgesIgnoringSafeArea(.all)
SafeBottomBlurContainer(containedView: MyCustomViewWithWhateverHeight)
}
}
}
推荐答案
这是一种可能的方法,可以使用像
Here is possible approach, that gives possibility to use container like
struct ContentView: View {
var body: some View {
ZStack {
Color.pink.edgesIgnoringSafeArea(.all)
// holder is not needed, because you can pass any view directly
SafeBottomBlurContainer(containedView: Text("Demo"))
}
}
}
和通用模糊容器
struct SafeBottomBlurContainer<V: View>: View {
@Environment(\.colorScheme) var colorScheme
var containedView: V
var body: some View {
GeometryReader { geometry in
VStack {
Spacer()
ZStack {
Blur(style: self.colorScheme == .dark ? .systemThinMaterialDark : .systemThinMaterialLight)
.frame(
width: geometry.size.width,
height: geometry.safeAreaInsets.bottom + 50
)
containedView
.padding(.bottom, geometry.safeAreaInsets.bottom)
}
}
.edgesIgnoringSafeArea(.bottom)
}
}
}
这篇关于如何在获取高度的同时将视图传递给结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!