0

I have a TabView within a ScrollView. Even though I give a fixed height to Color.orange of 500, the height of the TabView does not expand as it should.

The problem only appears when TabView is rendered within ScrollView.

struct TabViewProblems: View {
    
    var body: some View {
        ScrollView {
            TabView(selection: .constant(0), content:  {
                Color.orange.height(500)
            })
            .tabViewStyle(.page(indexDisplayMode: .always))
        }
    }
}

enter image description here

This problem does NOT exist when TabView or ScrollView are on there own.

struct TabViewProblems: View {
    
    var body: some View {
        ScrollView {
//            TabView(selection: .constant(0), content:  {
                Color.orange.height(500)
//            })
//            .tabViewStyle(.page(indexDisplayMode: .always))
        }
    }
}
struct TabViewProblems: View {
    
    var body: some View {
//        ScrollView {
            TabView(selection: .constant(0), content:  {
                Color.orange.height(500)
            })
            .tabViewStyle(.page(indexDisplayMode: .always))
//        }
    }
}

enter image description here

How can this be fixed? I don't want to give TabView a fixed height, I want it to assume the height of it's contents.

The accepted answer for this similar question says that "when you set an infinite height on object inside the scrollview it takes only the space that the content really needs." Here, the content explicitly needs a height of 500, but that still is not being respected.

Plato
  • 111
  • 2
  • 7
  • TabView should be the root view and is not intended to be inside other views. – koen Aug 04 '23 at 16:53
  • 1
    With SwiftUI parents decide the size of the children. If you want the child to determine size you have to build that logic. – lorem ipsum Aug 04 '23 at 20:16

0 Answers0