-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23863ff
commit 6b2abe1
Showing
1 changed file
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,48 @@ | ||
# swiftui-layout-guides | ||
Expose layout margins and readable content width to SwiftUI's Views | ||
# SwiftUI Layout Guides | ||
This micro-library exposes UIKit's layout margins and readable content guides to SwiftUI. | ||
|
||
## Usage | ||
### Make a view fit the readable content width | ||
Simply call the `fitToReadableContentWidth()` modifier: | ||
```swift | ||
List { | ||
ForEach(…) { | ||
Cell() | ||
.fitToReadableContentWidth() | ||
} | ||
} | ||
``` | ||
### Expose the layout margins in a block | ||
Wrap your view in the `WithLayoutMargins` view. The initializer supports two variants: one closure without argument and one closure with a `EdgeInsets` argument. In this last case, the insets correspond to the layout margins for the content: | ||
```swift | ||
WithLayoutMargins { layoutMarginsInsets in | ||
Text("ABC") | ||
.padding(.leading, layoutMarginsInsets.leading) | ||
} | ||
``` | ||
### Expose layout margins and readable content guides in a view | ||
You need two wrap your view in `WithLayoutMargins` (you can use the argument-less closure). This will populate the content's `Environment` with the layout margins and readable content in the form of insets. | ||
```swift | ||
WithLayoutMargins { | ||
Content() | ||
} | ||
|
||
struct Content: View { | ||
@Environment(\.layoutMarginsInsets) var layoutMarginsInsets | ||
@Environment(\.readableContentInsets) var readableContentInsets | ||
var body: some View { | ||
Text("ABC") | ||
.padding(.leading, layoutMarginsInsets.leading) | ||
… | ||
} | ||
} | ||
``` | ||
These insets are only valid for the bounds of the root content view. Using them deeper in the hierachy may lead to insconsitent results. | ||
|
||
## Installation | ||
Add `.package(url: "https://github.com/tgrapperon/swiftui-layout-guides", from: "0.0.2")` to your Package dependencies, and then | ||
``` | ||
.product(name: "SwiftUILayoutGuides", package: "swiftui-layout-guides") | ||
``` | ||
to your target's dependencies. | ||
|