问题描述
DynamicScalingView
是一个带有两个按钮的子视图,使用 preferencekey
问题:当 DynamicScalingView
嵌入到 NavigationView 中时,它不再适应 intrinsic
并增加其框架大小。没有导航的当前实现工作正常,但想了解如何在嵌入 NavigationView
XCode 12.2 和 iOS 14.2
struct SampleView: View {
var body: some View {
GeometryReader { gr in
//NavigationView {
ScrollView {
vstack {
// fills whatever space is left
Spacer()
.foregroundColor(.clear)
// view should fit with intrinsic content size
DynamicScalingView()
.padding(.horizontal,20)
.border(Color.blue,width: 1)
}
.padding(.bottom,20)
.border(Color.red,width: 1)
.frame(minHeight: gr.size.height)
.navigationBarHidden(true)
}
// }
}
}
struct DynamicScalingView: View {
@State private var labelHeight = CGFloat.zero
var body: some View {
HStack {
Button(action: {},label: {
Text("Some Text Some Text Some Text")
.padding(.horizontal,2)
})
.foregroundColor(Color.white)
.padding(.vertical)
.frame(minWidth: 0,maxWidth: .infinity)
.frame(minHeight: labelHeight)
.background(Color.blue)
.cornerRadius(8)
.fixedSize(horizontal: false,vertical: true)
.background(GeometryReader {
Color.clear
.preference(
key: ViewHeightKey.self,value: $0.frame(in: .local).size.height
)
})
Button(action: {},label: {
Text("Some Text")
.padding(.horizontal,value: $0.frame(in: .local).size.height
)
})
}
.onPreferenceChange(ViewHeightKey.self) {
self.labelHeight = $0
}
}
}
struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value,nextValue: () -> Value) {
value = max(value,nextValue())
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)