2

I have a TabView that is embeeded in a ZStack. My goal is to make the ZStack and it's child TabView to only take up the space needed to lay out the views inside the TabView. The .fixedSize() modifier accomplishes this if I change my TabView to a VStack but with a TabView it squishes the TabView too small. How can I accomplish my goal while still not obscuring the TabView's content?

enter image description here

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.red
            
            TabView {
                VStack {
                    Text("One")
                    Text("One")
                    Text("One")
                }
                VStack {
                    Text("Two")
                    Text("Two")
                    Text("Two")
                }
                VStack {
                    Text("Three")
                    Text("Three")
                    Text("Three")
                }
            }
            .tabViewStyle(.page)
            
        }
        
        .fixedSize() //why doesn't this work as expected?
        
    }
}
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • 1
    The answer to your question is that `TabViews`, like `ScrollViews`, `ListViews`, etc., have no intrinsic size. `.fixedSize` reduces a view size to its intrinsic, or natural, size. So, putting `.fixedSize` on them reduces their sizes beyond all recognition. The only thing that would reduce the `TabView` properly would be to put a `.frame()` on it. – Yrb Nov 07 '21 at 00:52
  • Thanks @Yrb but I don't want to use a hard coded frame height for reasons of different screen sizes and dynamic text sizes, what other options are there for this tabview to only be the size it needs to be? – GarySabo Nov 07 '21 at 18:15
  • While I can understand that you don't want to hard code them, you may be able to come up with some sort of value. I tried a solution with `PreferenceKeys` but it didn't work. I don't think they ever thought a `TabView` would be used like this. – Yrb Nov 07 '21 at 21:39

0 Answers0