You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+191Lines changed: 191 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,197 @@ Documentation
15
15
16
16
Documentation is automatically generated from source code comments and rendered as a static website hosted via GitHub Pages at: https://yml-org.github.io/YCoreUI/
17
17
18
+
Usage
19
+
----------
20
+
21
+
##1. UIView extensions for declarative Auto Layout
22
+
23
+
To aid in auto layout, Y—CoreUI has several `UIView` extensions that simplify creating layout constraints. These do not use any 3rd party library such as SnapKit, but are simply wrappers around Apple’s own `NSLayoutConstraint` api’s. If you are more comfortable using Apple’s layout constraint api’s natively, then by all means go ahead and use them. But these convenience methods allow for less verbose code that expresses its intent more directly.
24
+
25
+
All the extensions are to `UIView` and begin with the word `constrain`.
26
+
27
+
The simplest flavor just creates constraints using attributes just like the original iOS 6 `NSLayoutContraint` api.
28
+
29
+
30
+
```swift
31
+
//constrain a button's width to 100
32
+
let button =UIButton()
33
+
addSubview(button)
34
+
button.constrain(.width, constant: 100)
35
+
36
+
//constrain view to superview**
37
+
38
+
let container =UIView()
39
+
addSubview(container)
40
+
container.constrain(.leading, to: .leading, of: superview)
41
+
container.constrain(.trailing, to: .trailing, of: superview)
42
+
container.constrain(.top, to: .top, of: superview)
43
+
container.constrain(.bottom, to: .bottom, of: superview)
44
+
```
45
+
46
+
Another flavor creates constraints using anchors just like the anchor api’s first introduced in iOS 9.
47
+
48
+
```swift
49
+
//constrain a button's width to 100
50
+
let button =UIButton()
51
+
addSubview(button)
52
+
button.constrain(.widthAnchor, constant: 100)
53
+
54
+
// constrain view to superview**
55
+
56
+
let container =UIView()
57
+
addSubview(container)
58
+
container.constrain(.leadingAnchor, to: leadingAnchor)
59
+
container.constrain(.trailingAnchor, to: trailingAnchor)
60
+
container.constrain(.topAnchor, to: topAnchor)
61
+
container.constrain(.bottomAnchor, to: bottomAnchor)
62
+
```
63
+
64
+
There are overrides to handle the common use case of placing one view below another or to the trailing side of another:
65
+
66
+
```swift
67
+
// constrain button2.leadingAnchor to button1.trailingAnchor
button1.constrain(.widthAnchor, to: button2.widthAnchor)
88
+
89
+
// constrain view to superview
90
+
let container =UIView()
91
+
addSubview(container)
92
+
container.constrainEdges()
93
+
```
94
+
95
+
There’s also a `constrainEdgesToMargins` variant that sets constraints between the recipient’s edges and the layout margins of the specified view (typically the recipient’s superview). This is very handy for avoiding safe areas such as the region occupied by the navigation bar or by the FaceID notch.
96
+
97
+
```swift
98
+
// constrain 2 buttons across in a view using margins
button1.constrain(.widthAnchor, to: button2.widthAnchor)
109
+
110
+
// constrain view to superview margins
111
+
let container =UIView()
112
+
addSubview(container)
113
+
container.constrainEdgesToMargins()
114
+
```
115
+
116
+
## 2. UIColor extensions for WCAG 2.0 contrast ratio calculations
117
+
118
+
Y—CoreUI contains a number of extensions to make working with colors easier. The most useful of them may be WCAG 2.0 contrast calculations. Given any two colors (representing foreground and background colors), you can calculate the contrast ration between them and evaluate whether that passes particular WCAG 2.0 standards (AA or AAA). You can even write unit tests to quickly check all color pairs in your app across all color modes. That could look like this:
## 3. UIScrollView extensions to assist with keyboard avoidance
192
+
193
+
`FormViewController` is a view controller with a scrollable content area that will automatically avoid the keyboard for you. It is a good choice for views that have inputs (e.g. login or onboarding). Even for views without inputs, it is still quite useful for managing the creation of a `UIScrollView` and a `contentView` set within it, so that you can focus on your content and not have to code a scrollView for every view.
194
+
195
+
<aside>
196
+
💡 Almost every full-screen view in your app that contains any text should be a scroll view because of the vagaries of localization, Dynamic Type, potentially small screen sizes, and landscape mode support.
197
+
198
+
</aside>
199
+
200
+
## `UIScrollview` Extensions
201
+
202
+
Want to have a scrollview that avoids the keyboard, but you can’t use `FormViewController`? Most of its functionality is a simple extension to `UIScrollView`. You can add keyboard avoidance to any scroll view like so:
0 commit comments