Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Introduce ManualLayoutStackCellType
  • Loading branch information
muukii committed Nov 13, 2017
commit e83dc88cefb0269ea56ba2e02f505b0e0927b13c
109 changes: 109 additions & 0 deletions PinFlexLayoutDemo/PinFlexLayoutDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class ViewController: UIViewController {

views.append(MarginStackCell(height: 96, backgroundColor: marginColor))
views.append(FlexLabelStackCell(title: "Hello"))
views.append(PinLabelStackCell(title: "Fooo"))
views.append(DatePickerCell.init())
views.append(DatePickerCell.init())


stackScrollView.append(views: views)

Expand Down Expand Up @@ -66,6 +70,45 @@ final class MarginStackCell: StackCellBase {
}
}

// WIP:
final class PinLabelStackCell: StackCellBase, ManualLayoutStackCellType {

private let label = UILabel()

init(title: String) {
super.init()

addSubview(label)
label.font = UIFont.preferredFont(forTextStyle: .body)
label.text = title
}

override func layoutSubviews() {
super.layoutSubviews()

layout()
}

private func layout() {

label.pin.all().fitSize()

}

func size(maxWidth: CGFloat?, maxHeight: CGFloat?) -> CGSize {

if let maxWidth = maxWidth {
self.pin.width(maxWidth)
}

layout()

let size = self.bounds.size
return size
return CGSize(width: size.height, height: 16)
}
}

final class FlexLabelStackCell: StackCellBase, ManualLayoutStackCellType {

private let label = UILabel()
Expand Down Expand Up @@ -106,3 +149,69 @@ final class FlexLabelStackCell: StackCellBase, ManualLayoutStackCellType {
return size
}
}

final class DatePickerCell : StackCellBase, ManualLayoutStackCellType {

private let datePicker: UIDatePicker = .init()
private let label: UILabel = .init()
private let separator: UIView = .init()

private var isOn: Bool = false

override init() {
super.init()

self.addTarget(self, action: #selector(tap), for: .touchUpInside)

label.flex.height(40)
separator.flex.height(1 / UIScreen.main.scale)
backgroundColor = UIColor(white: 0.9, alpha: 1)
}

@objc func tap() {
isOn = !isOn
updateLayout(animated: true)
}

override func didMoveToSuperview() {
super.didMoveToSuperview()

superview?.backgroundColor = .black
}

override func layoutSubviews() {
super.layoutSubviews()

layout()
}

private func layout() {

self.flex.define { flex in
flex.addItem(label)

flex.addItem(separator)
flex.addItem(datePicker)

separator.flex.isIncludedInLayout(isOn)
datePicker.flex.isIncludedInLayout(isOn)

separator.alpha = isOn ? 1 : 0
datePicker.alpha = isOn ? 1 : 0
}

self.flex.layout(mode: .adjustHeight)
}

func size(maxWidth: CGFloat?, maxHeight: CGFloat?) -> CGSize {

if let maxWidth = maxWidth {
self.flex.width(maxWidth)
}

layout()

let size = self.bounds.size
return size
}
}
51 changes: 29 additions & 22 deletions StackScrollView/StackScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,35 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
}

precondition(cell.contentView.subviews.isEmpty)

view.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

cell.contentView.addSubview(view)

let top = view.topAnchor.constraint(equalTo: cell.contentView.topAnchor)
let right = view.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor)
let bottom = view.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
let left = view.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor)

top.identifier = LayoutKeys.top
right.identifier = LayoutKeys.right
bottom.identifier = LayoutKeys.bottom
left.identifier = LayoutKeys.left

NSLayoutConstraint.activate([
top,
right,
bottom,
left,
])

if view is ManualLayoutStackCellType {

cell.contentView.addSubview(view)

} else {

view.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

cell.contentView.addSubview(view)

let top = view.topAnchor.constraint(equalTo: cell.contentView.topAnchor)
let right = view.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor)
let bottom = view.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
let left = view.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor)

top.identifier = LayoutKeys.top
right.identifier = LayoutKeys.right
bottom.identifier = LayoutKeys.bottom
left.identifier = LayoutKeys.left

NSLayoutConstraint.activate([
top,
right,
bottom,
left,
])
}

return cell
}
Expand Down