Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI : How to Fix leading and trailing with margin 10

This is my first trial on SwiftUI, where I am trying to create a UITable view like UI. I am trying to give fix leading and trailing (not fix width) to cell/views, I have given ample of time and now this is what I have tried with output: enter image description here

Git : Here is the link to source code to reproduce this issue

like image 829
maddy Avatar asked Oct 05 '19 05:10

maddy


People also ask

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 does padding () do SwiftUI?

SwiftUI chooses a default amount of padding that's appropriate for the platform and the presentation context. To control the amount of padding independently for each edge, use padding(_:) . To pad all outside edges of a view by a specified amount, use padding(_:) .

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") .


Video Answer


1 Answers

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

{
            VStack(alignment: .center){
             ForEach(boxes) { box in
              BoxViewTable(box: box)
                .background(Color.white).padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
             }

After that, inside BoxViewTable, you need to add .frame modifier.

 HStack{

      Image("\(box.imgUrl)")
        .resizable()
        .frame(width: 80, height: 100, alignment: .leading)

      VStack(alignment:.leading){
        Text("\(box.newsTitle)")
          .lineLimit(2)
        Text("\(box.newsSubTitle) - \(box.dateTime)")
      }

    }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)

Finally, Don't give up :-)

like image 65
Razib Mollick Avatar answered Sep 30 '22 04:09

Razib Mollick