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, keyboardLayout, 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.keyboardLayout`**: 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.keyboardLayout:

##### Property:
* **`pin.keyboardLayout: UIEdgeInset`**
PinLayout's `UIView.pin.keyboardLayout` 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.keyboardLayout.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
10 changes: 9 additions & 1 deletion Sources/PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public class PinLayout<View: Layoutable> {
}
apply()
}

#if os(iOS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detail, but you could move this block just below public var layoutMargins: PEdgeInsets, so the proprities are declared from the most useful property order? Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public var keyboardLayout: PEdgeInsets {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After more thinking, it would better match other properties if we would name it keyboardMargins

  • UIView.readableContentGuide is named pin.readableMargins
  • UIView.layoutMarginsGuide is named pin.layoutMargins

Then I would propose

  • UIView.keyboardLayoutGuide named pin.keyboardMargins

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

guard #available(iOS 15.0, *) else { return .zero }
guard let view = view as? UIView else { return .zero }
return view.keyboardLayoutGuide
}
#endif

#if os(iOS) || os(tvOS)
public var safeArea: PEdgeInsets {
Expand All @@ -96,7 +104,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 Down