ios – Framing of HStack to suit father or mother view measurement with padding

0
22
ios – Framing of HStack to suit father or mother view measurement with padding


I’ve a view which is HStack of photographs with a horizontal padding. I need this HStack to slot in the father or mother view and fill it is width (together with padding). I’ve set a background colour of yellow after setting the padding. Downside is the background just isn’t seen on the display. I’m utilizing .containerRelativeFrame(.horizontal) and truthfully I do not precisely know the way it works because it provides unpredictable outcomes typically, so I’ve been experimenting placing this modifier in varied locations.

My query is how do I modify this code in order that your complete HStack with padding fills the father or mother view width?

enter image description here

import SwiftUI
import UIKit

extension UIImage {
    class func imageWithColor(colour: UIColor, measurement: CGSize=CGSize(width: 1, peak: 1)) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(measurement, false, 0)
        colour.setFill()
        UIRectFill(CGRect(origin: CGPoint.zero, measurement: measurement))
        let picture = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return picture!
    }
}

struct ThumbImage: Identifiable {
    var id = UUID()
    var picture:UIImage
}

struct TestHStack: View {
    @State var photographs:[ThumbImage] = []
    @State var numImages:Int = 15
    
    var physique: some View {
        VStack {
            
            HStack(alignment: .middle, spacing: 0) {
                ForEach(photographs) { thumbImage in
                    Picture(uiImage: thumbImage.picture)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .body(width: 50, peak: 50)
                        .clipped()
                }
            }
            .padding(.horizontal, 20)
            .background(Shade.yellow)
            
        }
        .containerRelativeFrame(.horizontal)
        .onGeometryChange(for: CGFloat.self, of: { proxy in
            proxy.measurement.width
        }, motion: { newValue in
            numImages = Int((newValue)/50)
            print("numImages = (numImages), (newValue)")
        })
        .onAppear(carry out: {
            print("On Seem")
            generateThumbs()
        })
        .onChange(of: numImages, { oldValue, newValue in
            print("On Change")
            generateThumbs()
            
        })
    }
    
    func generateThumbs() {
        var thumbs:[ThumbImage] = []
        var altColor:Shade = .pink
        for _ in 0.. ThumbImage {
        let uiColor = UIColor(colour)
        let uiImage = UIImage.imageWithColor(colour: uiColor, measurement: CGSize(width: 50, peak: 50))
        
        return ThumbImage(picture: uiImage)
        
    }
}

#Preview {
    TestHStack()
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here