Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support keyboardLayoutGuide #238

Merged
merged 12 commits into from
Feb 1, 2022
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Example/.DS_Store
Binary file not shown.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1221,16 +1221,17 @@ This example layout an UIImageView at the top and center it horizontally, it als


<a name="safeAreaInsets"></a>
## safeArea, readable and layout margins
## safeArea, keyboardMargins, readable and layout margins

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

1. **`UIView.pin.safeArea`**: Expose UIKit `UIView.safeAreaInsets` / `UIView.safeAreaLayoutGuide`.
2. **`UIView.pin.readableMargins`**: Expose UIKit `UIView.readableContentGuide`.
3. **`UIView.pin.layoutMargins`**: Expose UIKit `UIView.layoutMargins` / `UIView.layoutMarginsGuide`.
4. **`UIView.pin.keyboardMargins`**: Expose UIKit `UIView.keyboardLayoutGuide`.

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

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

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

<br/>

### 4. pin.keyboardMargins:

##### Property:
* **`pin.keyboardMargins: UIEdgeInset`**
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.

Bottom of safe area when the keyboard undocked.

This property can be used from iOS 15 and above.

##### Usage example:
```swift
container.pin.bottom(view.pin.keyboardMargins.top)
```


<a name="wrapContent"></a>
## WrapContent

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

* The newly pinned attributes have already been set to another value.
Example:
Expand Down
17 changes: 16 additions & 1 deletion Sources/PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class PinLayout<View: Layoutable> {
return .zero
}
}

public var readableMargins: PEdgeInsets {
guard #available(iOS 9.0, *) else { return .zero }
guard let view = view as? UIView else { return .zero }
Expand All @@ -114,6 +114,21 @@ public class PinLayout<View: Layoutable> {
return view.layoutMargins
}
#endif

#if os(iOS)
public var keyboardMargins: PEdgeInsets {
guard #available(iOS 15.0, *) else { return .zero }
guard let view = view as? UIView else { return .zero }

let layoutFrame = view.keyboardLayoutGuide.layoutFrame
guard !layoutFrame.isEmpty else { return .zero }

return UIEdgeInsets(top: layoutFrame.origin.y,
left: layoutFrame.origin.x,
bottom: view.frame.height - layoutFrame.origin.y - layoutFrame.height,
right: view.frame.width - layoutFrame.origin.x - layoutFrame.width)
}
#endif

//
// MARK: Layout using distances from superview’s edges
Expand Down