Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views compressed by other views in SwiftUI VStack and List

Tags:

In my SwiftUI application, I'm trying to implement a UI similar to this:

I've added the two rows for category 1 and category 2. The result looks like this:

NavigationView {     VStack(alignment: .leading) {         CategoryRow(...)         CategoryRow(...)         Spacer()     }     .navigationBarTitle(Text("Featured")) } 

Now, when added the view for the third category – an VStack with images – the following happens:

This happened, after I replaced Spacer(), with said VStack:

VStack(alignment: .leading) {     Text("Rivers")         .font(.headline)     ForEach(self.categories["Rivers"]!.identified(by: \.self)) { landmark in         landmark.image(forSize: 200)     } } 

My CategoryRow is implemented as follows:

VStack(alignment: .leading) {     Text(title)         .font(.headline)     ScrollView {         HStack {             ForEach(landmarks) { landmark in                 CategoryItem(landmark: landmark, isRounded: self.isRounded)             }         }     } } 

Question

It seems that the views are compressed. I was not able to find any compression resistance or content hugging priority modifiers to fix this.
I also tried to use .fixedSize() and .frame(width:height:) on CategoryRow.

How can I prevent the compression of these views?


Update

I've tried embedding the whole outer stack view in a scroll view:

NavigationView {     ScrollView { // also tried List         VStack(alignment: .leading) {             CategoryRow(...)             CategoryRow(...)             ForEach(...) { landmark in                 landmark.image(forSize: 200)             }         }         .navigationBarTitle(Text("Featured"))     } } 

...and the result is worse:

like image 431
LinusGeffarth Avatar asked Jun 08 '19 14:06

LinusGeffarth


People also ask

What is VStack and HStack in SwiftUI?

VStack, a vertical stack, which shows views in a top-to-bottom list. HStack, a horizontal stack, which shows views in a left-to-right list. ZStack, a depth-based stack, which shows views in a back-to-front list.

Is VStack a view?

Overview. Individually, HStack , VStack , and ZStack are simple views. HStack positions views in a horizontal line, VStack positions them in a vertical line, and ZStack overlays views on top of one another.

How many views can HStack SwiftUI have?

HStack can contain up to 10 static views, if you need more static views, you can nest HStack inside another HStack or Group to nest views inside.

How does VStack work SwiftUI?

Using stacks in SwiftUI allows you to arrange multiple views into a single coherent view with certain properties. HStack allows to arrange its child views in a horizontal line. VStack allows to arrange its child views in a vertical line, and ZStack allows to overlap its child views on top of each other.


1 Answers

You might prevent the views in VStack from being compressed by using

  .fixedSize(horizontal: false, vertical: true) 

For example: I have the following VStack:

VStack(alignment: .leading){         ForEach(group.items) {             FeedCell(item: $0)         }     } 

Which render compressed Text()

VStack with compressed elements

When I add .fixedSize(horizontal: false, vertical: true) it doesn't compress anymore

VStack(alignment: .leading){         ForEach(group.items) {             FeedCell(item: $0)                 .fixedSize(horizontal: false, vertical: true)         }     } 

VStack doesn't compresss content

like image 79
Denis Avatar answered Sep 28 '22 04:09

Denis