Sample project for testing out focus in SwiftUI and iOS 15

Overview

This project was to test out different ways of enabling focus in a SwiftUI app. Specifically I wanted to figure out how to force focus on newly created TextFields so that when they were added focus was set on them.

The secondary objective was to enable a user to click a button next to an existing text field and return focus to that particular field. The key was that the object backing each text field also had a UUID associated with the backing string. This UUID is what was being used to track which field got focus.

I looked at 5 ways of accomplishing this.

  1. Custom @Environment of type UUID (ContentViewEnvironment.swift)
  2. Custom @EnvironmentObject wrapping a published UUID (ContentViewEnvironmentObject.swift)
  3. Passing a FocusState<UUID?>.Binding variable to each view (ContentViewVariablePassing.swift)
  4. Custom @EnvironmentObejct wrapping a focus binding FocusState<UUID?>.Binding (ContentViewBindingEnvironmentObject.swift)
  5. Custom @Environment of type FocusState<UUID?>.Binding (ContentViewBindingEnvironment.swift)

What I found is that: Solution #1 was able to set focus on each new TextField but was not able to change the focus based on the button click.

Solution #2 enabled both required features but required each TextField implement onAppear and onReceive for the solution to work correctly.

Solution #3 enabled both required features but required that each view store and pass the binding down to subviews even if that view didn't need the behaviors.

Solution #4 enabled both required features but did not have the detractions of #2 or #3.

Solution #5 I could not get working.

You might also like...
Multivariate & A/B Testing for iOS and Mac

This library is no longer being maintained. You can continue to use SkyLab in your projects, but we recommend switching another solution whenever you

Remote configuration and A/B Testing framework for iOS

MSActiveConfig v1.0.1 Remote configuration and A/B Testing framework for iOS. Documentation available online. MSActiveConfig at a glance One of the bi

The Swift (and Objective-C) testing framework.
The Swift (and Objective-C) testing framework.

Quick is a behavior-driven development framework for Swift and Objective-C. Inspired by RSpec, Specta, and Ginkgo. // Swift import Quick import Nimbl

UI Testing Cheat Sheet and Examples.

UI Testing Cheat Sheet This repository is complementary code for my post, UI Testing Cheat Sheet and Examples. The post goes into more detail with exa

Swifty tool for visual testing iPhone and iPad apps. Every pixel counts.

Cribble Cribble - a tool for visual testing iPhone and iPad apps. Every pixel counts. Getting Started An example app is included demonstrating Cribble

T - A simple testing framework using closures and errors

t Quickly test expectations What is t? t is a simple testing framework using clo

AB testing framework for iOS

ABKit Split Testing for Swift. ABKit is a library for implementing a simple Split Test that: Doesn't require an HTTP client written in Pure Swift Inst

Keep It Functional - An iOS Functional Testing Framework
Keep It Functional - An iOS Functional Testing Framework

IMPORTANT! Even though KIF is used to test your UI, you need to add it to your Unit Test target, not your UI Test target. The magic of KIF is that it

An understated approach to iOS integration testing.
An understated approach to iOS integration testing.

Subliminal is a framework for writing iOS integration tests. Subliminal provides a familiar OCUnit/XCTest-like interface to Apple's UIAutomation frame

Owner
null
SwiftCheck is a testing library that automatically generates random data for testing of program properties

SwiftCheck QuickCheck for Swift. For those already familiar with the Haskell library, check out the source. For everybody else, see the Tutorial Playg

TypeLift 1.4k Dec 21, 2022
Testing the UI without UI Testing, a Swift experiment.

UI tests without UI Testing experiment This repo is a small experiment to see if there's an "in-between" for testing iOS applications. More feature-le

Joe Masilotti 20 Sep 26, 2022
Runtime introspection and unit testing of SwiftUI views

ViewInspector ??️‍♂️ for SwiftUI ViewInspector is a library for unit testing SwiftUI views. It allows for traversing a view hierarchy at runtime provi

Alexey Naumov 1.5k Jan 8, 2023
SwiftUI & Scenekit hit testing example.

HitTestApp SwiftUI & Scenekit hit testing example. This example application is written for macOS, though with minor changes it should be able to be ch

Eric Freitas 8 Oct 27, 2022
Automatic testing of your Pull Requests on GitHub and BitBucket using Xcode Server. Keep your team productive and safe. Get up and running in minutes. @buildasaur

Buildasaur Automatic testing of your Pull Requests on GitHub and BitBucket using Xcode Server. Keep your team productive and safe. Get up and running

Buildasaurs 774 Dec 11, 2022
Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5 and iOS 15.

StoreHelper Demo Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5, iOS 15. See also In-App Purchases with Xcode 12 and

Russell Archer 192 Dec 17, 2022
A flexible mock server for automated and regression testing of iOS, Android and other apps.

Note: This document is intended as a quick introduction to Voodoo. As Voodoo has a large number of features, please refer to Voodoo's Github Wiki for

Derek Clarkson 7 Nov 23, 2022
I built this application with unit testing and test-driven development to understand TDD theory and practice

TestDrivenDevelopment Description I built this application with unit testing and test-driven development to understand TDD theory and practice, to wri

null 1 Dec 21, 2021
Snapshot testing tool for iOS and tvOS

SnapshotTest is a simple view testing tool written completely in Swift to aid with development for Apple platforms. It's like unit testing for views.

Pär Strindevall 44 Sep 29, 2022
A Mac and iOS Playgrounds Unit Testing library based on Nimble.

Spry Spry is a Swift Playgrounds Unit Testing library based on Nimble. The best thing about Spry is that the API matches Nimble perfectly. Which means

Quick 327 Jul 24, 2022