[英]TabView with PageTabViewStyle() not using available height in ScrollView
so I have a TabView like shown below, but when I try to implement it in a ScrollView I always have to give it a fixed height.所以我有一个如下所示的 TabView,但是当我尝试在 ScrollView 中实现它时,我总是必须给它一个固定的高度。 Is there a way of telling the tabview to use the space it needs in the scrollView?有没有办法告诉 tabview 使用它在 scrollView 中需要的空间? I don't know the height of the inner content (which btw. changes over time) of the TabView, so a fixed height doesn't work for me.我不知道 TabView 的内部内容的高度(顺便说一句。随时间变化),因此固定高度对我不起作用。
I tried .frame(maxHeight: .infinity), but that doesn't seem to work我试过 .frame(maxHeight: .infinity),但这似乎不起作用
import SwiftUI
struct TEST: View {
var body: some View {
ScrollView {
TabView {
Text("Hello World")
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.frame(maxHeight: .infinity)
// only .frame() with fixed Height works...
}
}
}
You need to use a fixed height like the screen height because the scrollview has infinite height.您需要使用固定高度,如屏幕高度,因为滚动视图具有无限高度。 So when you set an infinite height on object inside the scrollview it takes only the space that the content really needs.因此,当您在滚动视图内的对象上设置无限高度时,它只占用内容真正需要的空间。 I recommend you to use a the screen height on your TabView like this :我建议您像这样在 TabView 上使用屏幕高度:
.frame(height: UIScreen.main.bounds.height) .frame(高度:UIScreen.main.bounds.height)
Or to use a GeometryReader that gives you the height taken by the ScrollView and then apply it to the TabView frame like this :或者使用 GeometryReader 为您提供 ScrollView 所采用的高度,然后将其应用到 TabView 框架,如下所示:
struct MyView: View {
var body: some View {
GeometryReader { proxy in
ScrollView {
TabView {
Text("Hello World")
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.frame(height: proxy.size.height)
}
}
}
}
Even if your question was asked a long time ago, I imagine you found a way around, but I think that response mite help others who encounter the issue.即使您的问题是很久以前提出的,我想您已经找到了解决方法,但我认为响应螨可以帮助遇到问题的其他人。 🙂 🙂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.