Gab being caused by unknown element in swift design

Viewed 25

Ok I have tried previous suggestions of just placing it in another vstack but still my view is showing the rectangle at the mid of the screen is their any reason why, also the edges have no padding around them making it edge to edge which I don't want?

struct ProfileView : View
{
 var body: some View {
     GeometryReader{ geo in
         VStack {
             
             VStack(alignment: .trailing, spacing: 10) {
                 RoundedRectangle(cornerRadius: 20)
                     .stroke(Color.primary, lineWidth: 2)
                     .frame(width: geo.size.width, height: 200)
                 
                     .padding(.horizontal)
             }.frame(width: geo.size.width, height: geo.size.height) // <<=== Here        }
         }
         
     }
 }

My Views are being injected into the content area via this page

struct ContentView: View {
 var body: some View {
     GeometryReader { geometry in
    TabView{
        HomeView().tabItem {
                Image(systemName: "house")
                Text("Home")
                
            }
            
        ProfileView()
            .tabItem {
                Image(systemName: "person.circle.fill")
                Text("Profile")
                
            }
        ZStack {
        StatsView()
            .tabItem {
                Image(systemName: "murry").renderingMode(.original).padding()
                Text("Plus")
                
            }
        }  

        
        StatsView()
            .tabItem {
                Image(systemName: "dumbbell.fill").renderingMode(.original).padding()
                Text("Stats")
                
            }
        
        StatsView()
            .tabItem {
                Image(systemName: "dumbbell.fill").renderingMode(.original).padding()
                Text("Notes")
                
            }
    }
}.safeAreaInset(edge: .bottom, alignment: .center, spacing: 0) {
    Color.clear
        .frame(height: 20)
        .background(Material.bar)
 }
 }}

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() .previewInterfaceOrientation(.landscapeLeft) } }

On Simlualtor and real device its placing it in the middle of the screen.

I want this frame so that I can place other elements in side it is a rounded rectangle the best way to go for this?

enter image description here

1 Answers

You are using GeometryReader as the outer most view in the ProfileView, and GeometryReader has the behavior to take as much space as there is available, if you want your rectangle to show in the top or bottom of the screen consider using a Spacer() and if you want to add padding to your rectangle so it does not go to the edges of the screen try removing the .frame(width: geo.size.width) because you are forcing the rectangle to be the width of the screen, in nature the rectangle will take as much space as it can so you do not have to specify that.

This is your code but with some tweaks:

struct ProfileView : View {
var body: some View {
    VStack {
        
        RoundedRectangle(cornerRadius: 20)
            .stroke(Color.primary, lineWidth: 2)
            .frame(height: 200)
            .padding(.horizontal)
        
        Spacer()
    }
}

}

Related