diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Example/.DS_Store b/Example/.DS_Store new file mode 100644 index 0000000..b066f01 Binary files /dev/null and b/Example/.DS_Store differ diff --git a/README.md b/README.md index 25c95bb..5464759 100644 --- a/README.md +++ b/README.md @@ -1221,16 +1221,17 @@ This example layout an UIImageView at the top and center it horizontally, it als -## 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) @@ -1360,6 +1361,22 @@ PinLayout's `UIView.pin.layoutMargins` property expose directly the value of UIK
+### 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) +``` + + ## WrapContent @@ -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: diff --git a/Sources/PinLayout.swift b/Sources/PinLayout.swift index b0df003..e614b16 100644 --- a/Sources/PinLayout.swift +++ b/Sources/PinLayout.swift @@ -96,7 +96,7 @@ public class PinLayout { return .zero } } - + public var readableMargins: PEdgeInsets { guard #available(iOS 9.0, *) else { return .zero } guard let view = view as? UIView else { return .zero } @@ -114,6 +114,21 @@ public class PinLayout { 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