• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    Swift
  • Created about 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for learning how the SwiftUI layout system works, and for debugging your own layout code.

SwiftUI Layout Inspector

https://github.com/ole/swiftui-layout-inspector

A Swift library (and iOS/Mac app) for learning how the SwiftUI layout system works, and for debugging your own layout code. Inspect the layout behavior of SwiftUI views, i.e. what sizes views propose to their children and how a view determines its own size.

Layout Inspector screenshot on iPhone simulator

Motivation

At its core, SwiftUI’s layout algorithm is wonderfully simple:

  1. The parent view proposes a size to its child view(s). Width and height are both optional; either one (or both) can be nil.

  2. The child view determines its own size, taking the proposed size into account, as well as the sizes of its own children (it’s a recursive process).

  3. The child reports its size back to the parent. The parent can’t change the child’s size (in SwiftUI, each view determines its own size).

  4. The parent view positions its children.

Complex layouts in SwiftUI can be achieved by composing built-in views and view modifiers. The tricky part about understanding the layout system is learning the layout behaviors of the built-in views, many of which are poorly documented (as of November 2022). The goal of this package is to help you learn.

Components

Layout Inspector consists of:

  • The LayoutInspector library, provided as a SwiftPM package. Add it to your own SwiftUI app to debug your layout code.

  • The LayoutInspectorDemo app, an iOS and Mac app that shows Layout Inspector in action.

Requirements

iOS 16.0 or macOS 13.0 (because it requires the Layout protocol).

Instructions

  1. import LayoutInspector

  2. At the top of the view tree you want to inspect, insert .inspectLayout().

  3. Insert .layoutStep("View label") at each point in a view tree where you want to inspect the layout algorithm (what sizes are being proposed and returned). This is necessary to inject the helper "views" that observe the layout process.

Acknowledgements

Idea and initial code based on: objc.io, Swift Talk episode 318, Inspecting SwiftUI's Layout Process (2022-08-19)

Runtime warnings in Xcode: Point-Free, Unobtrusive runtime warnings for libraries (2022-01-03)

See also Credits.md

More Repositories

1

whats-new-in-swift-4

An Xcode playground showcasing the new features in Swift 4.0.
Swift
1,827
star
2

OBShapedButton

A UIButton subclass that works with for non-rectangular button shapes.
Objective-C
946
star
3

whats-new-in-swift-4-2

An Xcode playground demonstrating the new features in in Swift 4.2.
Swift
872
star
4

OLEContainerScrollView

A UIScrollView subclass that intelligently handles multiple child scroll views and does not interfere with UIKitʼs cell reuse functionality.
Objective-C
588
star
5

Animated-Paths

Demo project: Animating the drawing of a CGPath with CAShapeLayer.strokeEnd
Objective-C
505
star
6

swiftui-view-lifecycle

Observe how different SwiftUI container views affect state and lifecycle events (onAppear, onDisappear).
Swift
374
star
7

OBSlider

A UISlider subclass that adds variable scrubbing speeds (as seen in the Music app on iOS).
Objective-C
267
star
8

CollectionViewParallaxScrolling

A custom UICollectionViewFlowLayout to support parallax scrolling in cells. Check out the accompanying blog post for more info.
Objective-C
250
star
9

Ampere

Adding multiplication and division to the units of measurement types in Foundation.
Swift
171
star
10

SortedArray

An array that keeps its elements sorted according to a given sort predicate.
Swift
165
star
11

CustomScrollView

A very simple implementation of a scroll view on iOS. Not meant for production, just for learning purposes. Please check out the accompanying blog post.
Objective-C
154
star
12

OBGradientView

A simple UIView wrapper for CAGradientLayer. For the times when it's more convenient to use a view instead of a CALayer.
Objective-C
112
star
13

NSBundle-OBCodeSigningInfo

Check an app bundle's code signing and sandboxing state to NSBundle.
Objective-C
106
star
14

swift-rp-pico-bare

Embedded Swift on the Raspberry Pi Pico without the Pico C/C++ SDK
C
93
star
15

Storyboard-Strings-Extraction

Automatically extract translatable strings from Xcode storyboards and update .strings files.
Shell
74
star
16

SilentPush

An iOS test app for testing silent push notifications and background fetching.
Swift
71
star
17

CGPathHitTesting

A demo iOS app to illustrate hit testing with CGPathCreateCopyByStrokingPath()
Objective-C
71
star
18

pico-embedded-swift

Embedded Swift on the Raspberry Pi Pico
CMake
62
star
19

AppointmentList

Sample iOS app that lists events from the device's calendar store in a table view and groups them by date.
Objective-C
48
star
20

RemoteViewControllers

Introspection of the private and undocumented remote view controllers feature in iOS 6
Objective-C
45
star
21

iphone-6-plus-rendering

Quick and dirty test apps to understand how the built-in downsampling to screen resolution works on the iPhone 6 Plus.
Swift
44
star
22

GestureRecognition

Sample iOS app for a blog post about gesture recognition and attention to detail
Objective-C
42
star
23

itunesconnect-financialreports

Converts Financial Reports from iTunes Connect (App Store) to HTML
Ruby
37
star
24

OSLogStoreTest

Test app for evaluating if apps can access their own OSLog logs via OSLogStore.
Swift
30
star
25

AnalogDial

A circular analog dial that can display a numeric value in a specified range (like an analog speedometer).
Swift
24
star
26

AppleCoreDataModels

21
star
27

NotificationUnregistering

Testing the behavior of the block based NotificationCenter API. Do you have to call NotificationCenter.removeObserver(_:) or is it enough to let the observer token go out of scope? Turns out: the former (tested on iOS 11.2).
Swift
16
star
28

ConcurrencyBenchmark

Swift
15
star
29

HexHexHex

A parser for the Intel Hexadecimal Object File Format (.hex), written in Swift.
Swift
14
star
30

Kaffeekasse

Sample apps for a Passbook tutorial on my blog.
C++
14
star
31

transcribe

A Swift parser for output files from automated transcription services. An experiment inspired by the Swift Community Podcast.
Swift
13
star
32

Picobello

An assembler for the Microchip PIC instruction set, written in Swift.
Swift
13
star
33

topaz

An attempt to reimplement a minimal version of the Swift standard library. Just for fun — I doubt this has practical applications.
Swift
12
star
34

RGBtoCMYK

A simple command-line Cocoa app to convert images to CMYK
Objective-C
10
star
35

SwiftUI-Mac-Focus

Test project for playing with focus, keyboard shortcuts, menu commands in SwiftUI on the Mac
Swift
10
star
36

CAShapeLayer-Animation

Demo project for a Stack Overflow question: "Animate CAShapeLayer path on animated bounds change"
Objective-C
9
star
37

Funtime

A Swift interface for the Objective-C Runtime API
Swift
8
star
38

CollectionViewContentSizeAnimation

Demo project for a Stack Overflow question.
Objective-C
8
star
39

SwiftPM-embedded-minimal

Swift
6
star
40

OBStack

OBStack is a very simple implementation of a stack data structure (last in, first out) in Cocoa.
Objective-C
5
star
41

AsyncSequenceOperators

Experimental reimplementations of functional reactive operators for AsyncSequence. DO NOT USE IN PRODUCTION!
Swift
5
star
42

AsyncXMLParsing

An AsyncSequence wrapper for Foundation’s XMLParser (experimental)
Swift
4
star
43

SwiftUIAnimationBug

https://twitter.com/olebegemann/status/1587912494290444290
Swift
3
star
44

twitter-backup

A Ruby script to backup tweets (in the raw JSON format)
Ruby
3
star
45

rust-rp-pico-template

A cargo-generate template for embedded Rust development on the Raspberry Pi Pico.
Rust
2
star
46

docopt.objc

Incomplete beginnings of an implementation of the docopt language in Objective-C.
Objective-C
2
star
47

CoreDataDebugging

Swift
2
star
48

SetSolver

A solver for the card game Set.
Swift
1
star
49

SwiftConcurrencyDeadlock

Swift
1
star