Swiftpack.co - tgrapperon/swiftui-layout-guides as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
tgrapperon/swiftui-layout-guides
Expose layout margins and readable content width to SwiftUI's Views
.package(url: "https://github.com/tgrapperon/swiftui-layout-guides.git", from: "0.0.2")

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:

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:

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.

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 and you should use the measureLayoutMargins() modifier if you want to refresh the insets for the target view.

Installation

Add .package(url: "https://github.com/tgrapperon/swiftui-layout-guides", from: "0.0.1") to your Package dependencies, and then

.product(name: "SwiftUILayoutGuides", package: "swiftui-layout-guides")

to your target's dependencies.

GitHub

link
Stars: 1
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics