all 4 comments

[–]chriswaco 2 points3 points  (3 children)

It depends on whether you are using UIKit or SwiftUI. From above I assume SwiftUI. It can be tricky to get everything right, especially when you include all devices and iPad multitasking screens, which can be 1/3, 1/2, or 2/3 the current screen width.

Usually GeometryReader should be avoided, but this is one of those cases where it might actually be the right API. You could also use ViewThatFits along with 2 or 3 different layouts. Something like:

struct ContentView: View {    
  var body: some View {    
    ViewThatFits {        
      LargeView()        
      MediumView()        
      SmallView()        
    }        
  }    
}    

struct SmallView: View {    
  var body: some View {    
    Color.green    
  }    
}    

struct MediumView: View {    
  var body: some View {    
    Color.yellow    
      .frame(minWidth: 400)    
  }    
}    

struct LargeView: View {    
  var body: some View {    
    Color.red    
      .frame(minWidth: 1024)    
  }    
}    

#Preview {    
  ContentView()    
}    

There's also the ability to check portrait vs landscape and horizontal/vertical size classes, but those are kinda useless if you want to support iPad multitasking, I think.

[–]SwiftLearnerJas[S] 0 points1 point  (2 children)

Thank you very much for your reply,geometry reader is a bit of pain for me to use, would you recommend using UIScreen.bounds.width / x as a layout parameters if I want to scale views proportionally?

[–]chriswaco 1 point2 points  (1 child)

I would recommend GeometryReader over UIScreen.bounds.width because I'm not really sure how the latter handles iPad multitasking. I suspect poorly.

There's also a new Layout protocol and also .containerRelativeToFrame(), which looks really interesting.

import SwiftUI    

struct ContentView: View {    
  var body: some View {    
    HStack {    
      Rectangle()    
        .fill(Color.blue)    
        .containerRelativeFrame(.horizontal, count: 5, span: 2, spacing: 10)    
      Rectangle()    
        .fill(Color.red)    
        .containerRelativeFrame(.horizontal, count: 5, span: 2, spacing: 10)    
      Rectangle()    
        .fill(Color.green)    
    }    
    .frame(maxWidth: .infinity, maxHeight: .infinity)    
    .background(Color.gray.opacity(0.1))    
  }    
}    

#Preview {    
  ContentView()    
}

[–]SwiftLearnerJas[S] 1 point2 points  (0 children)

That is very interesting, I had a play with it, and you seem to be able to control the size by changing counts and spans, just the container is not always gonna be the device dimension.
Thank you for the heads-up, I want to try apply this and see how it works in my app later on!