Skip to content

Commit e83dc88

Browse files
committed
Introduce ManualLayoutStackCellType
1 parent b568158 commit e83dc88

File tree

2 files changed

+138
-22
lines changed

2 files changed

+138
-22
lines changed

PinFlexLayoutDemo/PinFlexLayoutDemo/ViewController.swift

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class ViewController: UIViewController {
2525

2626
views.append(MarginStackCell(height: 96, backgroundColor: marginColor))
2727
views.append(FlexLabelStackCell(title: "Hello"))
28+
views.append(PinLabelStackCell(title: "Fooo"))
29+
views.append(DatePickerCell.init())
30+
views.append(DatePickerCell.init())
31+
2832

2933
stackScrollView.append(views: views)
3034

@@ -66,6 +70,45 @@ final class MarginStackCell: StackCellBase {
6670
}
6771
}
6872

73+
// WIP:
74+
final class PinLabelStackCell: StackCellBase, ManualLayoutStackCellType {
75+
76+
private let label = UILabel()
77+
78+
init(title: String) {
79+
super.init()
80+
81+
addSubview(label)
82+
label.font = UIFont.preferredFont(forTextStyle: .body)
83+
label.text = title
84+
}
85+
86+
override func layoutSubviews() {
87+
super.layoutSubviews()
88+
89+
layout()
90+
}
91+
92+
private func layout() {
93+
94+
label.pin.all().fitSize()
95+
96+
}
97+
98+
func size(maxWidth: CGFloat?, maxHeight: CGFloat?) -> CGSize {
99+
100+
if let maxWidth = maxWidth {
101+
self.pin.width(maxWidth)
102+
}
103+
104+
layout()
105+
106+
let size = self.bounds.size
107+
return size
108+
return CGSize(width: size.height, height: 16)
109+
}
110+
}
111+
69112
final class FlexLabelStackCell: StackCellBase, ManualLayoutStackCellType {
70113

71114
private let label = UILabel()
@@ -106,3 +149,69 @@ final class FlexLabelStackCell: StackCellBase, ManualLayoutStackCellType {
106149
return size
107150
}
108151
}
152+
153+
final class DatePickerCell : StackCellBase, ManualLayoutStackCellType {
154+
155+
private let datePicker: UIDatePicker = .init()
156+
private let label: UILabel = .init()
157+
private let separator: UIView = .init()
158+
159+
private var isOn: Bool = false
160+
161+
override init() {
162+
super.init()
163+
164+
self.addTarget(self, action: #selector(tap), for: .touchUpInside)
165+
166+
label.flex.height(40)
167+
separator.flex.height(1 / UIScreen.main.scale)
168+
backgroundColor = UIColor(white: 0.9, alpha: 1)
169+
}
170+
171+
@objc func tap() {
172+
isOn = !isOn
173+
updateLayout(animated: true)
174+
}
175+
176+
override func didMoveToSuperview() {
177+
super.didMoveToSuperview()
178+
179+
superview?.backgroundColor = .black
180+
}
181+
182+
override func layoutSubviews() {
183+
super.layoutSubviews()
184+
185+
layout()
186+
}
187+
188+
private func layout() {
189+
190+
self.flex.define { flex in
191+
flex.addItem(label)
192+
193+
flex.addItem(separator)
194+
flex.addItem(datePicker)
195+
196+
separator.flex.isIncludedInLayout(isOn)
197+
datePicker.flex.isIncludedInLayout(isOn)
198+
199+
separator.alpha = isOn ? 1 : 0
200+
datePicker.alpha = isOn ? 1 : 0
201+
}
202+
203+
self.flex.layout(mode: .adjustHeight)
204+
}
205+
206+
func size(maxWidth: CGFloat?, maxHeight: CGFloat?) -> CGSize {
207+
208+
if let maxWidth = maxWidth {
209+
self.flex.width(maxWidth)
210+
}
211+
212+
layout()
213+
214+
let size = self.bounds.size
215+
return size
216+
}
217+
}

StackScrollView/StackScrollView.swift

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,35 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
180180
}
181181

182182
precondition(cell.contentView.subviews.isEmpty)
183-
184-
view.translatesAutoresizingMaskIntoConstraints = false
185-
cell.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
186-
187-
cell.contentView.addSubview(view)
188-
189-
let top = view.topAnchor.constraint(equalTo: cell.contentView.topAnchor)
190-
let right = view.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor)
191-
let bottom = view.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
192-
let left = view.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor)
193-
194-
top.identifier = LayoutKeys.top
195-
right.identifier = LayoutKeys.right
196-
bottom.identifier = LayoutKeys.bottom
197-
left.identifier = LayoutKeys.left
198-
199-
NSLayoutConstraint.activate([
200-
top,
201-
right,
202-
bottom,
203-
left,
204-
])
183+
184+
if view is ManualLayoutStackCellType {
185+
186+
cell.contentView.addSubview(view)
187+
188+
} else {
189+
190+
view.translatesAutoresizingMaskIntoConstraints = false
191+
cell.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
192+
193+
cell.contentView.addSubview(view)
194+
195+
let top = view.topAnchor.constraint(equalTo: cell.contentView.topAnchor)
196+
let right = view.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor)
197+
let bottom = view.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
198+
let left = view.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor)
199+
200+
top.identifier = LayoutKeys.top
201+
right.identifier = LayoutKeys.right
202+
bottom.identifier = LayoutKeys.bottom
203+
left.identifier = LayoutKeys.left
204+
205+
NSLayoutConstraint.activate([
206+
top,
207+
right,
208+
bottom,
209+
left,
210+
])
211+
}
205212

206213
return cell
207214
}

0 commit comments

Comments
 (0)