简体   繁体   English

带有 PageTabViewStyle() 的 TabView 不使用 ScrollView 中的可用高度

[英]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.

相关问题 带有 PageTabViewStyle 的 TabView 上的edgesIgnoringSafeArea 不起作用 - edgesIgnoringSafeArea on TabView with PageTabViewStyle not working 带有 PageTabViewStyle 的 TabView 中的 Memory 泄漏 - Memory leak in TabView with PageTabViewStyle 带有 PageTabViewStyle 的 TabView 屏幕中的背景未填充整个可用垂直空间 - Background in screens in TabView w/ PageTabViewStyle doesn't fill entire vertical space available SwiftUI TabView PageTabViewStyle 防止更改标签? - SwiftUI TabView PageTabViewStyle prevent changing tab? SwiftUI TabView PageTabViewStyle 崩溃而不显示任何错误 - SwiftUI TabView PageTabViewStyle crashes without showing any error 如何在 SwiftUI 中使用 PageTabViewStyle 摆脱 TabView 上的索引点? - How can I get rid of index dots on TabView with PageTabViewStyle in SwiftUI? 带有 TabView 的水平分页滚动视图,SwiftUI - Horizontal Paging Scrollview with TabView, SwiftUI "SwiftUI TabView 与 PageTabViewStyle 在设备上的横向与 safeArea 添加奇数前沿插图" - SwiftUI TabView with PageTabViewStyle in Landscape on device with safeArea adding odd leading edge inset 使用自动布局增加Scrollview中的视图高度 - Increase View Height In Scrollview Using Auto Layout 隐藏 ScrollView 内的 SwiftUI LazyHStack TabView - SwiftUI LazyHStack TabView inside ScrollView is hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM