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
*[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)
15
16
*[System Spacing on iOS and tvOS](#system-spacing-on-ios-and-tvos)
16
17
17
18
## Why Oh Why?
@@ -302,13 +303,13 @@ item >> layoutSize(100, 50).min // at least 100 by 50
302
303
item >> .min(100, 50) // same
303
304
```
304
305
305
-
## Constrain to Parent
306
+
## Constrain to the Parent
306
307
307
308
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:
308
309
309
310
```swift
310
311
classMySuperview: UIView {
311
-
// ... other code including call to addSubviews() ...
312
+
// ... other code, including call to addSubviews() ...
312
313
313
314
funcaddSubviews() {
314
315
let subview =addForAutoLayout(UIView())
@@ -318,11 +319,11 @@ class MySuperview: UIView {
318
319
}
319
320
```
320
321
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:
322
323
323
324
```swift
324
325
classMySuperview: UIView {
325
-
// ... other code including call to addSubviews() ...
326
+
// ... other code, including call to addSubviews() ...
326
327
327
328
funcaddSubviews() {
328
329
let subview =addForAutoLayout(UIView())
@@ -337,11 +338,30 @@ If you still want to explicitly constrain a layout item to its parent, you can u
337
338
338
339
```swift
339
340
item >> item.parent?.top.offset(10) // constrain top to parent, inset 10
341
+
item >> item.parent?.allButBottom// constrain 3 edges to parent
340
342
item >> item.parent?.size.at(0.3) // constrain width and height to 30% of parent
341
343
item >> item.parent?.all(leadingOffset: 10) // constrain all edges to parent, leading inset 10
342
344
item >> item.parent// constrain all edges to parent
343
345
```
344
346
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
+
classMyView: UIView {
355
+
// ... other code, including call to addSubviews() ...
356
+
357
+
funcaddSubviews() {
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
+
345
365
## System Spacing on iOS and tvOS
346
366
347
367
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
0 commit comments