Code Monkey home page Code Monkey logo

swiftuifocusguide's Introduction

SwiftUIFocusGuide

The missing UIFocusGuide for SwiftUI

Why?

Although Apple added some limited focus support in 2020 (WWDC20-10042), it is still lacking behind it's UIFocusGuide counterpart.

Requirements

  • iOS 14.0 or greater
  • tvOS 14.0 or greater
  • macOS 10.15 or greater

Usage

import SwiftUI
import SwiftUIFocusGuide

struct SimpleExampleView: View {

    @StateObject var focusBag = SwiftUIFocusBag()

    var body: some View {
        HStack(spacing: 0) {
            VStack {
                Button(action: {}, label: { Text("Button") })
                Spacer()
            }
            .addFocusGuide(using: focusBag, name: "Left", destinations: [.right: "Right"], debug: true)

            VStack {
                Spacer()
                Button(action: {}, label: { Text("Button") })
            }
            .addFocusGuide(using: focusBag, name: "Right", destinations: [.left: "Left"], debug: true)
        }
    }
}

The above example looks like this:

Simple Example

Passing debug: true visualizes the focus guides on the screen.

A more complex example can be found in the Example tvOS app.

How it works

SwiftUIFocusGuide wraps its content in a UIHostingController and setups a focus guide for each direction. It then sets the preferredFocusEnvironments according to the specified destination by name.

public func updateUIViewController(_ uiViewController: UIHostingController<Content>, context: Context) {
    ...
    for (direction, destination) in destinations {
        ... 
        if let destinationView = bag.views[destination.name] {
            context.coordinator.guides[direction]?.preferredFocusEnvironments = bag.isEnabled ? [destinationView] : []
        }
    }
}

For a deeper understanding of how UIFocusGuide works, I recommend this article.

Limitations

Using UIHostingController has its caveats: It does not wrap its intrinsic content size. As a result, the focus guide takes up all available space. You would have to set a fixed size with the frame() modifier and set its intrinsic content size manually.

See:

swiftuifocusguide's People

Contributors

rmnblm avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.