For the width:
They have already got the identical width, however you cant see it since you apply the peak after setting the background. So reorder the modifiers to make it seen:
struct TestViewHStack: View {
let strings = ["Short", "some long text", "short"]
var physique: some View {
HStack {
ForEach(strings, id: .self) { title in
CardView(title: title)
// .body(maxWidth: .infinity) // 👈 That is after the background framing
}
}
}
}
struct CardView: View {
let title: String
var physique: some View {
VStack(spacing: 8) {
Picture(systemName: "star.fill")
.resizable()
.body(width: 20, top: 20)
Textual content(title)
.font(.subheadline)
}
.padding(16)
.body(maxWidth: .infinity) // 👈 This does the job as anticipated
.background(Coloration.white)
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(coloration: .grey.opacity(0.2), radius: 8, x: 0, y: 2)
}
}
Seen Equal Widths Demo:
For the peak:
In case you simply want a hard and fast top, simply add a maxHeight
to the body and management the peak of the stack.
However it looks as if you do not know the peak and also you want all objects to have the identical dimension with the tallest one. So it’s essential allow them to dimension themselves and replace anybody with the shorter top. That is the place onPreferenceChange
is available in and requires some work.
struct TestViewHStack: View {
let strings = ["Short", "some long text", "short"]
@State non-public var maxCardHeight: CGFloat = 10
var physique: some View {
HStack {
ForEach(strings, id: .self) { title in
CardView(title: title)
.fixedSize(horizontal: false, vertical: true)
.background {
GeometryReader { proxy in
Coloration.clear.choice(
key: MaxValue.self,
worth: proxy.dimension.top
)
}
}
.onPreferenceChange(MaxValue.self) {
maxCardHeight = $0
}
.body(maxWidth: .infinity, maxHeight: maxCardHeight)
.background(Coloration.white)
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(coloration: .grey.opacity(0.2), radius: 8, x: 0, y: 2)
}
}
}
}
public enum MaxValue: PreferenceKey {
public static let defaultValue = CGFloat()
public static func scale back(worth: inout Worth, nextValue: () -> Worth) {
print("worth: (worth), subsequent: (nextValue())")
worth = max(worth, nextValue())
}
}
struct CardView: View {
let title: String
var physique: some View {
VStack(spacing: 8) {
Picture(systemName: "star.fill")
.resizable()
.body(width: 20, top: 20)
Textual content(title)
.font(.subheadline)
}
.padding(16)
}
}
Word that you will need to first dimension after which apply the background. So It’s worthwhile to rearrange your UI logic
boAt Nirvana Ion TWS Earbuds with 120 HRS Playback(24hrs/Charge), Crystal Bionic Sound with Dual EQ Modes, Quad Mics ENxâ„¢ Technology, Low Latency(60ms), in Ear Detection(Charcoal Black)
₹1,698.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Fastrack Limitless Glide Advanced UltraVU HD Display|BT Calling|ATS Chipset|100+ Sports Modes & Watchfaces|Calculator|Voice Assistant|in-Built Games|24 * 7 HRM|IP68 Smartwatch
₹1,399.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)realme Buds 2 Wired in Ear Earphones with Mic (Black)
₹599.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Portronics iKonnect C Pro Type C to 3.5 mm Audio Jack Connector with DAC Headphone Converter Adapter Compatible with iPhone 15 Pro Max/15 Pro/15 Plus, Galaxy S23/S22/S21/S208 & Other Type C Phones
₹219.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)POCO C55 (Cool Blue, 6GB RAM, 128GB Storage)
₹6,499.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Wayona Nylon Braided USB to Lightning Fast Charging and Data Sync Cable Compatible for iPhone 13, 12,11, X, 8, 7, 6, 5, iPad Air, Pro, Mini (3 FT Pack of 1, Grey)
₹399.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Oakter Mini UPS for 12V WiFi Router Broadband Modem | Backup Upto 4 Hours | WiFi Router UPS Power Backup During Power Cuts | UPS Broadband Modem | Current Surge & Deep Discharge Protection
₹1,399.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Ambrane Unbreakable 60W / 3A Fast Charging 1.5m Braided Micro USB Cable for Smartphones, Tablets, Laptops & other Micro USB devices, 480Mbps Data Sync, Quick Charge 3.0 (RCM15, Black)
₹149.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Zebronics-NS1500 Laptop Stand Featuring Foldable Design, Anti-Slip Silicone Rubber Pads, Supports Maximum of 5kgs Weight Tabletop
₹299.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Toysbuddy Re-Writable LCD Writing Tablet Pad with Screen 21.5cm (8.5Inch) for Drawing, Playing, Handwriting Best Birthday Gifts for Adults & Kids Girls Boys, Multicolor
₹139.00 (as of April 1, 2024 14:09 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Auto Amazon Links: No products found.