Skip to content

Commit f6d9bbc

Browse files
authored
Merge pull request #26 from iWECon/dev
dev
2 parents a581b0d + 34f0f5f commit f6d9bbc

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Sources/StackKit/HStackView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ open class HStackView: UIView, StackView {
148148
}
149149

150150
open override func sizeToFit() {
151-
frame.size = sizeThatFits(.zero)
151+
_fitSize(with: self._stackKit_fitType)
152152
}
153153

154154
open override var intrinsicContentSize: CGSize {

Sources/StackKit/UIView+StackKit/UIView+FitSize.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ public enum FitType {
88
case width
99
case height
1010

11+
/// Set fixed height, width auto calculate
1112
case widthFlexible
13+
/// Set fixed width, height auto calculate
1214
case heightFlexible
1315

1416
var isFlexible: Bool {
@@ -88,22 +90,34 @@ extension UIView: FitSize {
8890
let sizeThatFits = sizeThatFits(CGSize(width: fitWidth, height: fitHeight))
8991

9092
switch fitType {
91-
case .width, .height, .widthFlexible, .heightFlexible:
93+
case .width, .height:
9294
if fitWidth != .greatestFiniteMagnitude {
93-
size.width = fitType.isFlexible ? sizeThatFits.width : fitWidth
95+
size.width = fitWidth
9496
} else {
9597
size.width = sizeThatFits.width
9698
}
9799

98100
if fitHeight != .greatestFiniteMagnitude {
99-
size.height = fitType.isFlexible ? sizeThatFits.height : fitHeight
101+
size.height = fitHeight
100102
} else {
101103
size.height = sizeThatFits.height
102104
}
105+
106+
case .widthFlexible:
107+
size.height = fitHeight
108+
size.width = sizeThatFits.width
109+
110+
case .heightFlexible:
111+
size.height = fitType.isFlexible ? sizeThatFits.height : fitHeight
112+
size.width = fitWidth
113+
103114
case .content:
104115
size = Size(width: sizeThatFits.width, height: sizeThatFits.height)
105116
}
106117

118+
// fix when set single width or height not working
119+
_fixedSize(&size)
120+
107121
size.width = applyMinMax(toWidth: size.width)
108122
size.height = applyMinMax(toHeight: size.height)
109123

Sources/StackKit/VStackView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ open class VStackView: UIView, StackView {
148148
}
149149

150150
open override func sizeToFit() {
151-
frame.size = sizeThatFits(.zero)
151+
_fitSize(with: self._stackKit_fitType)
152152
}
153153

154154
open override var intrinsicContentSize: CGSize {

Tests/StackKitTests/StackKitTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,42 @@ final class StackKitTests: XCTestCase {
66
// This is an example of a functional test case.
77
// Use XCTAssert and related functions to verify your tests produce the correct
88
// results.
9+
10+
let label = UILabel()
11+
let container = VStackView(alignment: .center, distribution: .spacing(2), padding: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)) {
12+
label.stack.then {
13+
$0.text = "你好啊"
14+
}
15+
}
16+
let v = VStackView(alignment: .center, distribution: .fillWidth(spacing: 0), padding: .zero) {
17+
container.stack.height(50)
18+
}
19+
20+
v.layoutSubviews()
21+
print(label.frame.size)
22+
print(container.frame.size)
23+
XCTAssertEqual(label.frame.minY, (container.frame.height - label.frame.height) / 2)
24+
}
25+
26+
func testCenter() throws {
27+
28+
let label = UILabel()
29+
let vcontainer = VStackView(alignment: .center, distribution: .spacing(2)) {
30+
label.stack.then {
31+
$0.text = "你好啊"
32+
}
33+
}
34+
let container = HStackView(alignment: .center, padding: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)) {
35+
vcontainer
36+
}
37+
container.stack.height(50)
38+
container.sizeToFit()
39+
40+
XCTAssertEqual(vcontainer.center.x, container.center.x)
41+
42+
container.stack.height(nil)
43+
vcontainer.stack.height(50)
44+
container.sizeToFit()
45+
XCTAssertEqual(vcontainer.center.x, container.center.x)
946
}
1047
}

0 commit comments

Comments
 (0)