Skip to content

Commit 2e7f584

Browse files
authored
Merge pull request #238 from baekteun/master
support keyboardLayoutGuide
2 parents 92d1e23 + 3dbf6f8 commit 2e7f584

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,17 @@ This example layout an UIImageView at the top and center it horizontally, it als
12211221

12221222

12231223
<a name="safeAreaInsets"></a>
1224-
## safeArea, readable and layout margins
1224+
## safeArea, keyboardMargins, readable and layout margins
12251225

1226-
UIKit expose 3 kind of areas/guides that can be used to layout views.
1226+
UIKit expose 4 kind of areas/guides that can be used to layout views.
12271227
PinLayout expose them using these properties:
12281228

12291229
1. **`UIView.pin.safeArea`**: Expose UIKit `UIView.safeAreaInsets` / `UIView.safeAreaLayoutGuide`.
12301230
2. **`UIView.pin.readableMargins`**: Expose UIKit `UIView.readableContentGuide`.
12311231
3. **`UIView.pin.layoutMargins`**: Expose UIKit `UIView.layoutMargins` / `UIView.layoutMarginsGuide`.
1232+
4. **`UIView.pin.keyboardMargins`**: Expose UIKit `UIView.keyboardLayoutGuide`.
12321233

1233-
The following image display the 3 areas on an iPad in landscape mode.
1234+
The following image display the 3 areas on an iPad in landscape mode. (safeArea, readableMargins, layoutMargins)
12341235

12351236
<img src="docs/images/pinlayout_example_layout_margins_landscape.png" width="440" />
12361237

@@ -1360,6 +1361,22 @@ PinLayout's `UIView.pin.layoutMargins` property expose directly the value of UIK
13601361

13611362
<br/>
13621363

1364+
### 4. pin.keyboardMargins:
1365+
1366+
##### Property:
1367+
* **`pin.keyboardMargins: UIEdgeInset`**
1368+
PinLayout's `UIView.pin.keyboardMargins` property expose directly the value of UIKit [`UIView.keyboardLayoutGuide`](https://developer.apple.com/documentation/uikit/keyboards_and_input/adjusting_your_layout_with_keyboard_layout_guide). This is really useful when layout adjustment due to the keyboard is required.
1369+
1370+
Bottom of safe area when the keyboard undocked.
1371+
1372+
This property can be used from iOS 15 and above.
1373+
1374+
##### Usage example:
1375+
```swift
1376+
container.pin.bottom(view.pin.keyboardMargins.top)
1377+
```
1378+
1379+
13631380
<a name="wrapContent"></a>
13641381
## WrapContent
13651382

@@ -1624,7 +1641,7 @@ PinLayout can display warnings in the console when pin rules cannot be applied o
16241641
* The newly pinned attributes conflict with other already pinned attributes.
16251642
Example:
16261643
`view.pin.left(10).right(10).width(200)`
1627-
👉 Layout Conflict: `width(200) won't be applied since it conflicts with the following already set properties: left: 0, right: 10.`
1644+
👉 Layout Conflict: `width(200) won't be applied since it conflicts with the following already set properties: left: 0, right: 10.`
16281645

16291646
* The newly pinned attributes have already been set to another value.
16301647
Example:

Sources/PinLayout.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class PinLayout<View: Layoutable> {
9696
return .zero
9797
}
9898
}
99-
99+
100100
public var readableMargins: PEdgeInsets {
101101
guard #available(iOS 9.0, *) else { return .zero }
102102
guard let view = view as? UIView else { return .zero }
@@ -114,6 +114,21 @@ public class PinLayout<View: Layoutable> {
114114
return view.layoutMargins
115115
}
116116
#endif
117+
118+
#if os(iOS)
119+
public var keyboardMargins: PEdgeInsets {
120+
guard #available(iOS 15.0, *) else { return .zero }
121+
guard let view = view as? UIView else { return .zero }
122+
123+
let layoutFrame = view.keyboardLayoutGuide.layoutFrame
124+
guard !layoutFrame.isEmpty else { return .zero }
125+
126+
return UIEdgeInsets(top: layoutFrame.origin.y,
127+
left: layoutFrame.origin.x,
128+
bottom: view.frame.height - layoutFrame.origin.y - layoutFrame.height,
129+
right: view.frame.width - layoutFrame.origin.x - layoutFrame.width)
130+
}
131+
#endif
117132

118133
//
119134
// MARK: Layout using distances from superview’s edges

0 commit comments

Comments
 (0)