Skip to content

Commit b8b87b7

Browse files
author
flowtoolz
committed
Document how to constrain views to safe areas
1 parent 26a5b0c commit b8b87b7

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ GetLaid is a lean framework for laying out complex UIs through short readable co
1111
* [Constrain Multiple Positions](#constrain-multiple-positions)
1212
* [Constrain Dimensions](#constrain-dimensions)
1313
* [Constrain Both Dimensions](#constrain-both-dimensions)
14-
* [Constrain to Parent](#constrain-to-parent)
14+
* [Constrain to the Parent](#constrain-to-the-parent)
15+
* [Constrain to the Safe Area on iOS](#constrain-to-the-safe-area-on-ios)
1516
* [System Spacing on iOS and tvOS](#system-spacing-on-ios-and-tvos)
1617

1718
## Why Oh Why?
@@ -302,13 +303,13 @@ item >> layoutSize(100, 50).min // at least 100 by 50
302303
item >> .min(100, 50) // same
303304
```
304305

305-
## Constrain to Parent
306+
## Constrain to the Parent
306307

307308
Normally, in well structured code, views add and layout their own subviews. In those contexts, the parent (superview) of the constrained subviews is `self`, which makes it easy to constrain those subviews to any of their parent's attributes:
308309

309310
```swift
310311
class MySuperview: UIView {
311-
// ... other code including call to addSubviews() ...
312+
// ... other code, including call to addSubviews() ...
312313

313314
func addSubviews() {
314315
let subview = addForAutoLayout(UIView())
@@ -318,11 +319,11 @@ class MySuperview: UIView {
318319
}
319320
```
320321

321-
Sometimes, not all superviews are implemented as their own custom view class. In other words, some custom view- or controller classes add and layout not just sibling subviews but whole subview hierarchies. In those contexts, the enclosing custom view or view controller controls the parent-child relation of its subviews and can directly constrain suviews to their parents:
322+
Sometimes, not all superviews are implemented as their own custom view class. In other words, some custom view- or controller classes add and layout not just sibling subviews but whole subview hierarchies. In those contexts, the enclosing custom view or view controller controls the parent-child relation of its subviews and can directly constrain subviews to their parents:
322323

323324
```swift
324325
class MySuperview: UIView {
325-
// ... other code including call to addSubviews() ...
326+
// ... other code, including call to addSubviews() ...
326327

327328
func addSubviews() {
328329
let subview = addForAutoLayout(UIView())
@@ -337,11 +338,30 @@ If you still want to explicitly constrain a layout item to its parent, you can u
337338

338339
```swift
339340
item >> item.parent?.top.offset(10) // constrain top to parent, inset 10
341+
item >> item.parent?.allButBottom // constrain 3 edges to parent
340342
item >> item.parent?.size.at(0.3) // constrain width and height to 30% of parent
341343
item >> item.parent?.all(leadingOffset: 10) // constrain all edges to parent, leading inset 10
342344
item >> item.parent // constrain all edges to parent
343345
```
344346

347+
## Constrain to the Safe Area on iOS
348+
349+
On iOS 11 and above, you can access the safe area of a view via the `safeArea` property and the parent's safe area via the optional `parentSafeArea` property.
350+
351+
Normally, in well structured code where views add and layout their own subviews, you would simply call `safeArea` on `self`:
352+
353+
```swift
354+
class MyView: UIView {
355+
// ... other code, including call to addSubviews() ...
356+
357+
func addSubviews() {
358+
addForAutoLayout(MyContentView()) >> safeArea // constrain content to safe area
359+
}
360+
}
361+
```
362+
363+
If you find youself constraining many subviews to the safe area, there should probably be a content view containing them.
364+
345365
## System Spacing on iOS and tvOS
346366

347367
With Apple's `NSLayoutAnchor`, you can make use of a mysterious "system spacing". Apple does not disclose how that is calculated and does not offer any concrete values you could access. Using system spacings through the `NSLayoutAnchor` API is a bit awkward, limited in how it is applied and limited in what it can be applied to.
@@ -365,7 +385,6 @@ Remember that these constants are not hardcoded but dynamically calculated on th
365385

366386
## TO DOcument
367387

368-
* safe areas
369388
* shorten and update motivational introduction
370389

371390
[badge-pod]: https://img.shields.io/cocoapods/v/GetLaid.svg?label=version&style=flat-square

0 commit comments

Comments
 (0)