-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScrollableDistrib.swift
146 lines (107 loc) · 4.03 KB
/
ScrollableDistrib.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// An iOS control that makes content scroll vertically.
//
// https://github.com/marketplacer/Scrollable
//
// This file was automatically generated by combining multiple Swift source files.
//
// ----------------------------
//
// Scrollable.swift
//
// ----------------------------
import UIKit
/**
Makes the content in the scroll view scrollable.
*/
open class Scrollable {
@discardableResult
open class func createContentView(_ scrollView: UIScrollView) -> UIView {
let contentView = UIView()
scrollView.addSubview(contentView)
Scrollable.embedSubviews(scrollView, inNewSuperview: contentView)
Scrollable.layoutContentViewInScrollView(contentView, scrollView: scrollView)
scrollView.layoutIfNeeded()
return contentView
}
class func layoutContentViewInScrollView(_ contentView: UIView, scrollView: UIView) {
// Make content view fill scroll view
ScrollableAutolayoutConstraints.fillParent(contentView, parentView: scrollView, margin: 0,
vertically: false)
ScrollableAutolayoutConstraints.fillParent(contentView, parentView: scrollView, margin: 0,
vertically: true)
// Content view width is equal to scroll view width
ScrollableAutolayoutConstraints.equalWidth(contentView, viewTwo: scrollView,
constraintContainer: scrollView)
}
class func embedSubviews(_ fromView: UIView, inNewSuperview newSuperview: UIView) {
newSuperview.translatesAutoresizingMaskIntoConstraints = false
// Move all subviews to newSuperview
for subview in fromView.subviews {
if subview == newSuperview { continue }
newSuperview.addSubview(subview)
}
// Move all scrollview constraints to contentView
moveConstraints(fromView, toView: newSuperview)
}
class func moveConstraints(_ fromView: UIView, toView: UIView) {
let constraints = fromView.constraints
for constraint in constraints {
moveConstraint(constraint, fromView: fromView, toView: toView)
}
}
fileprivate class func moveConstraint(_ constraint: NSLayoutConstraint,
fromView: UIView, toView: UIView) {
if let firstItem = constraint.firstItem as? NSObject {
let newFirstItem = firstItem == fromView ? toView : firstItem
if let secondItem = constraint.secondItem as? NSObject {
let newSecondItem = secondItem == fromView ? toView : secondItem
let newConstraint = NSLayoutConstraint(
item: newFirstItem,
attribute: constraint.firstAttribute,
relatedBy: constraint.relation,
toItem: newSecondItem,
attribute: constraint.secondAttribute,
multiplier: constraint.multiplier,
constant: constraint.constant
)
newConstraint.priority = constraint.priority
fromView.removeConstraint(constraint)
toView.addConstraint(newConstraint)
}
}
}
}
// ----------------------------
//
// ScrollableAutolayoutConstraints.swift
//
// ----------------------------
//
// Collection of shortcuts to create autolayout constraints.
//
import UIKit
class ScrollableAutolayoutConstraints {
class func fillParent(_ view: UIView, parentView: UIView, margin: CGFloat = 0, vertically: Bool = false) {
var marginFormat = ""
if margin != 0 {
marginFormat = "-\(margin)-"
}
var format = "|\(marginFormat)[view]\(marginFormat)|"
if vertically {
format = "V:" + format
}
let constraints = NSLayoutConstraint.constraints(withVisualFormat: format,
options: [], metrics: nil,
views: ["view": view])
parentView.addConstraints(constraints)
}
@discardableResult
class func equalWidth(_ viewOne: UIView, viewTwo: UIView, constraintContainer: UIView) -> [NSLayoutConstraint] {
let constraints = NSLayoutConstraint.constraints(withVisualFormat: "[viewOne(==viewTwo)]",
options: [], metrics: nil,
views: ["viewOne": viewOne, "viewTwo": viewTwo])
constraintContainer.addConstraints(constraints)
return constraints
}
}