Skip to content

Commit 1e1f304

Browse files
authored
fix: 🐛 Card layout return small height some times (SAP#1182)
This happens when cards are put into a List.
1 parent a061c93 commit 1e1f304

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

Apps/Examples/Examples/FioriSwiftUICore/Card/MobileCardExample.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,44 @@ struct MobileCardExample: View {
1717
}
1818
.cardStyle(.card)
1919
.listStyle(.plain)
20-
.navigationBarTitle("Cards", displayMode: .inline)
20+
.navigationBarTitle("Cards in List", displayMode: .inline)
2121
} label: {
22-
Text("Cards")
22+
Text("Cards in List")
2323
}
2424

25+
NavigationLink {
26+
ScrollView(.vertical) {
27+
VStack(alignment: .leading, spacing: 8) {
28+
ForEach(0 ..< CardTests.cardSamples.count, id: \.self) { i in
29+
CardTests.cardSamples[i]
30+
}
31+
.background(Color.preferredColor(.primaryGroupedBackground))
32+
}
33+
}
34+
.cardStyle(.card)
35+
.navigationBarTitle("Cards in VStack", displayMode: .inline)
36+
} label: {
37+
Text("Cards in VStack")
38+
}
39+
40+
NavigationLink {
41+
ScrollView(.horizontal) {
42+
HStack(alignment: .top, spacing: 8) {
43+
ForEach(0 ..< CardTests.cardSamples.count, id: \.self) { i in
44+
CardTests.cardSamples[i]
45+
.cardStyle(.card)
46+
.cardStyle(.intrinsicHeightCard)
47+
.padding()
48+
}
49+
.background(Color.preferredColor(.primaryGroupedBackground))
50+
}
51+
}
52+
.cardStyle(.card)
53+
.navigationBarTitle("Cards in HStack", displayMode: .inline)
54+
} label: {
55+
Text("Cards in HStack")
56+
}
57+
2558
NavigationLink {
2659
List {
2760
ForEach(0 ..< CardTests.cardFooterSamples.count, id: \.self) { i in

Sources/FioriSwiftUICore/_FioriStyles/CardMainHeaderStyle.fiori.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extension CardMainHeaderFioriStyle {
6565
// Add default style for Title
6666
.foregroundStyle(Color.preferredColor(self.isLoading ? .separator : .primaryLabel))
6767
.font(.fiori(forTextStyle: .title3, weight: .bold))
68+
.multilineTextAlignment(.leading)
6869
.environment(\.numberOfLines, 3)
6970
}
7071
}
@@ -77,6 +78,7 @@ extension CardMainHeaderFioriStyle {
7778
// Add default style for Subtitle
7879
.foregroundStyle(Color.preferredColor(self.isLoading ? .separator : .secondaryLabel))
7980
.font(.fiori(forTextStyle: .body))
81+
.multilineTextAlignment(.leading)
8082
.environment(\.numberOfLines, 2)
8183
}
8284
}

Sources/FioriSwiftUICore/_FioriStyles/CardStyle.fiori.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,18 @@ struct CardLayout: Layout {
8080
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout CacheData) -> CGSize {
8181
self.calculateLayout(proposal: proposal, subviews: subviews, cache: &cache)
8282
let finalWidth = max(proposal.width ?? 0, cache.maxWidth)
83-
8483
return CGSize(width: finalWidth, height: cache.height)
8584
}
8685

8786
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout CacheData) {
8887
self.calculateLayout(proposal: proposal, subviews: subviews, cache: &cache)
89-
9088
for (i, subview) in subviews.enumerated() {
9189
let item = cache.rows[i]
9290

9391
var pt = CGPoint(x: item.origin.x + bounds.origin.x, y: item.origin.y + bounds.origin.y)
9492
/// If it is the footer and there is exessive height for the card then the footer is moved to the bottom
95-
if self.useProposedHeight, subview.priority == 3, item.origin.y + item.size.height < cache.height {
96-
pt.y = cache.height - item.size.height + bounds.origin.y
93+
if subview.priority == 3, item.origin.y + item.size.height < bounds.size.height {
94+
pt.y = bounds.size.height - item.size.height + bounds.origin.y
9795
}
9896
subview.place(at: pt, proposal: ProposedViewSize(width: item.size.width, height: nil))
9997
}
@@ -105,11 +103,9 @@ struct CardLayout: Layout {
105103

106104
func calculateLayout(proposal: ProposedViewSize, subviews: Subviews, cache: inout CacheData) {
107105
let containerWidth = proposal.width
108-
109106
let height: CGFloat
110-
111-
if self.useProposedHeight {
112-
height = proposal.height ?? (cache.rows.last?.maxY ?? 0)
107+
if self.useProposedHeight, let value = proposal.height, value > 0, value < CGFloat.greatestFiniteMagnitude {
108+
height = max(value, cache.height)
113109
} else {
114110
height = cache.rows.last?.maxY ?? 0
115111
}
@@ -141,8 +137,8 @@ struct CardLayout: Layout {
141137
}
142138

143139
cache.height = cache.rows.last?.maxY ?? 0
144-
if self.useProposedHeight, let value = proposal.height {
145-
cache.height = value
140+
if self.useProposedHeight, let value = proposal.height, value > 0, value < CGFloat.greatestFiniteMagnitude {
141+
cache.height = max(value, cache.height)
146142
}
147143
}
148144
}
@@ -584,7 +580,9 @@ public enum CardTests {
584580
}
585581
} cardBody: {
586582
HStack(alignment: .center, spacing: 4) {
587-
ChartView(CardTests.chartModel).frame(height: 168)
583+
ChartView(CardTests.chartModel)
584+
.frame(minWidth: 128)
585+
.frame(height: 168)
588586

589587
VStack(alignment: .leading, spacing: 8) {
590588
HStack {

0 commit comments

Comments
 (0)