Home > Blockchain >  Adjusting big or small screens using geometry reader
Adjusting big or small screens using geometry reader

Time:10-19

I am currently stuck in trouble i am not really able to resize image and texts to fit bigger or small screens. This is just a simple card that i want to be able to be resizable and should be in the same proportions. I have even tried geometry reader. I am having trouble as even if I have used geometry reader reader the white spaces I wanted are not there as the picture takes the entire width and height which looks bad not ideal. If there is a way for me to have the same proportion of the card for all different screen sizes then I am all open to ideas.

Image of my card:

Here is my code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Image("shops")
                .resizable()
                .aspectRatio(contentMode: .fill)
                
            
            VStack {
                
                Spacer()
                Text("Welcome to MarketInfo")
                
                    .font(.largeTitle)
                    .fontWeight(.semibold)
                    .foregroundColor(.white)

                
            }
            .padding(.bottom, 30)
            .frame(width: 400)
        }
        .frame(width: 380, height: 270)
        .cornerRadius(20)
        .clipped()
        .shadow(radius: 8)
        .padding(.top, 20)
        
}
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
        }
    }
}

CodePudding user response:

I think where you are running into issues is in using the fixed frame. The issue with using it is that it does not adjust for sizes. If you had something complicated, you could use a GeometryReader to resize the frame based on a proportion of the available space. However, the simplest implementation is setting your Text as an .overlay() on your image and setting the .aspectRatio(contentMode: .fit).

The code would now be:

struct ContentView: View {
    var body: some View {
        Image("shops")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .overlay(
                VStack {
                    Spacer()
                    Text("Welcome to MarketInfo")
                    
                        .font(.largeTitle)
                        .fontWeight(.semibold)
                        .foregroundColor(.white)
                }
            )
            .cornerRadius(20)
            .clipped()
            .shadow(radius: 8)
            .padding(30)
    }
}

The overlay is never bigger than the Image, which can be an issue using a ZStack, and it will move with the Image. I then just through some padding on the Image all around to keep it in from the sides, and away from other views.

CodePudding user response:


import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack() {
            Image("shops")
                .resizable()
                .aspectRatio(1.4, contentMode: .fit) // 380 / 270 = 1.4
                .overlay(
                    VStack {
                        Spacer()
                        Text("Welcome to MarketInfo")
                            .font(.largeTitle)
                            .fontWeight(.semibold)
                            .foregroundColor(.white)
                            .lineLimit(1)
                            .minimumScaleFactor(0.5)
                            .padding()
                    })
        }
        // .frame is not required, but Change .infinity to a number will resize the card
        .frame(maxWidth: .infinity)
        .cornerRadius(20)
        .shadow(radius: 8)
        .padding(.horizontal, 20)
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        
        ContentView()
            .previewDevice("iPhone 13 Pro")
        ContentView()
            .previewDevice("iPod touch (7th generation)")
        ContentView()
            .previewDevice("iPad Pro (12.9-inch) (5th generation)")
    }
}

  • Related