Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How to remove margin between views in VStack?

Tags:

swiftui

vstack

Using SwiftUI, I created a VStack, which contains some fixed elements and a list element. The reason is, that the user should only scroll the area under the fixed elements. Now I see a space between the second fixed element and the list. I don't know where this space is coming from and want to get rid of it, but have no idea, how. The area is marked in red.

struct DashboardView : View, CoreDataInjected {      var body: some View {         GeometryReader { geometry in             VStack {                 ScopeSelectorView().frame(maxWidth: .infinity).background(ColorPalette.gray)                 BalanceNumberView().frame(maxWidth: .infinity)                 List {                     DashboardNavigationView(                         height: geometry.size.height - ScopeSelectorView.height - BalanceNumberView.height                     ).frame(maxWidth: .infinity).listRowInsets(.zero)                 }             }         }.background(Color.red).edgesIgnoringSafeArea(.all)     } } 

Screenshot of view

like image 627
tosi Avatar asked Sep 19 '19 20:09

tosi


People also ask

What is VStack default spacing?

VStack and HStack already have a default spacing that differs across operating systems and it gets cumbersome using VStack(spacing: 0) { } or HStack(spacing: 0) { } across the views. You can create a handy kind-of syntactic sugar view with zero spacing by default.

How do I set margins in SwiftUI?

You need to use . padding modifier for margin. In your code, you have to add padding inside your ScrollView. After that, inside BoxViewTable, you need to add .

What is horizontal padding SwiftUI?

SwiftUI lets us set individual padding around views using the padding() modifier, causing views to be placed further away from their neighbors. If you use this with no parameters you'll get system-default padding on all sides, like this: VStack { Text("Using") Text("SwiftUI") .


2 Answers

Since you didn't pass a spacing argument to VStack, it is picking a default spacing based on context. If you want no spacing, pass 0 explicitly.

VStack(spacing: 0) {     // content here } 
like image 132
rob mayoff Avatar answered Sep 21 '22 06:09

rob mayoff


Separately

You can use offset modifier on any view to make it looks different for each content separately:

VStack {     Circle()     Circle().offset(x: 0, y: -20)     Circle().offset(x: 0, y: 40) } 

Note that it could be negative in both directions.


All at once

Also VStack and HStack have an argument called spacing and you can set it to 0 or any other number you need to apply it to all elements.

VStack(spacing: 0) {     Circle()     Circle() } 

Note that is could be negative if needed.

like image 29
Mojtaba Hosseini Avatar answered Sep 17 '22 06:09

Mojtaba Hosseini