Display CreditsView on Button

TODO: collapasable, copyable, download, etc

How can I have a second Window CreditsView in my WindowGroup? I don't want it to show inititially. Then if someone clicks "Credits" button in my MainView I will display it. Clicking anywhere on it, should dismiss that window Spatial computing goes even further with visionOS updates that bring enhanced support for volumetric apps, powerful new enterprise APIs, the new TabletopKit framework, and more.

visionOS Highlights

TabletopKit Create collaborative experiences centered around a table. This new framework handles the manipulation of cards and pieces, establishing placement and layout, and defining game boards.

New enterprise APIs provide access to spatial barcode scanning, the Apple Neural Engine, object tracking parameters, and more.

visionOS for enterprise

Volumetric APIs • Create apps that take full advantage of depth and space — and can

run side by side with other apps in the Shared Space.
• Resize volumes by using the SwiftUI scene

modifier windowResizability.

Decide if you want the user’s hands to appear in front of or behind your digital content.

Inputs

Getting started? Explore the visionOS Pathway on developer.apple.com 􀯻

• Detect planes in all orientations. • Allow anchoring objects on surfaces in your surroundings.
• Use Room Anchors to consider the user’s surroundings on a

per-room basis.
• Attach content to individual objects around the user with the new

Object Tracking API for visionOS.

Updates to scene understanding

To create a secondary window (CreditsView) in your WindowGroup that only displays when a button is clicked and dismisses when tapped, you can use the WindowGroup and state management in SwiftUI. Below is an example demonstrating how to achieve this:

Step-by-Step Implementation

  1. Create the CreditsView: This view will be displayed as a secondary window.

    import SwiftUI
    
    struct CreditsView: View {
        var dismiss: () -> Void
    
        var body: some View {
            VStack {
                Text("Credits")
                    .font(.largeTitle)
                    .padding()
                Spacer()
            }
            .contentShape(Rectangle())  // Make the entire view tappable
            .onTapGesture {
                dismiss()
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }
    
  2. Create the MainView: This view contains the button that will trigger the display of the CreditsView.

    import SwiftUI
    
    struct MainView: View {
        @State private var showCredits = false
    
        var body: some View {
            VStack {
                Button("Show Credits") {
                    showCredits = true
                }
                .padding()
                Text("Main Content")
                    .font(.title)
            }
            .sheet(isPresented: $showCredits) {
                CreditsView {
                    showCredits = false
                }
            }
        }
    }
    
  3. Set Up the WindowGroup: This is where you define your app's main entry point, which includes MainView and manages the state for displaying CreditsView.

    import SwiftUI
    
    @main
    struct MyApp: App {
        var body: some Scene {
            WindowGroup {
                MainView()
            }
        }
    }
    

Explanation

  • CreditsView: This view includes a dismiss closure that is called when the view is tapped. The contentShape(Rectangle()) modifier ensures the entire view is tappable.
  • MainView: Contains a Button that toggles the state variable showCredits. This state variable is bound to the sheet modifier, which presents the CreditsView when showCredits is true. The CreditsView calls the dismiss closure to hide itself.
  • MyApp: Sets up the main WindowGroup with MainView as the initial content.

This setup ensures that CreditsView is not shown initially. It only appears when the button in MainView is clicked and dismisses itself when tapped anywhere on the view.Is this the best setup when running on visionOs?For visionOS, you should use Window for creating multiple windows within a WindowGroup. The following is an updated implementation to suit visionOS's multi-window environment.

Updated Implementation for visionOS

  1. Create the CreditsView:

    import SwiftUI
    
    struct CreditsView: View {
        var dismiss: () -> Void
    
        var body: some View {
            VStack {
                Text("Credits")
                    .font(.largeTitle)
                    .padding()
                Spacer()
            }
            .contentShape(Rectangle())  // Make the entire view tappable
            .onTapGesture {
                dismiss()
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }
    
  2. Create the MainView: This view contains the button that will trigger the display of the CreditsView.

    import SwiftUI
    
    struct MainView: View {
        @Environment(\.openWindow) var openWindow
    
        var body: some View {
            VStack {
                Button("Show Credits") {
                    openWindow(id: "creditsWindow")
                }
                .padding()
                Text("Main Content")
                    .font(.title)
            }
        }
    }
    
  3. Set Up the WindowGroup and Additional Window: This setup ensures proper handling of multiple windows in visionOS.

    import SwiftUI
    
    @main
    struct MyApp: App {
        var body: some Scene {
            WindowGroup {
                MainView()
            }
            .windowStyle(.volumetric)  // Ensures the main view is volumetric
    
            Window("Credits Window", id: "creditsWindow") {
                CreditsView {
                    // Implement a method to close the window
                    NSApp.keyWindow?.close()
                }
            }
            .windowStyle(.volumetric)  // Ensures the credits window is volumetric
            .hidden()
        }
    }
    

Explanation

  • CreditsView: Remains the same as before, with a dismiss closure that will be called to close the window.
  • MainView: Uses the openWindow environment value to open the window with the specified ID.
  • MyApp: Defines the WindowGroup for the main window and an additional Window for the credits view.
    • The WindowGroup includes the main content view.
    • An additional Window with the ID "creditsWindow" is defined but is hidden initially.
    • The openWindow function in MainView triggers the display of the CreditsView.

Usage Notes

  • Window Styles: Using .volumetric ensures the windows take advantage of visionOS's 3D space.
  • Window Management: NSApp.keyWindow?.close() is used within the CreditsView to close the window when tapped.

This implementation leverages visionOS's capability to manage multiple windows more effectively, ensuring that the credits window can be opened and closed appropriately.In swift UI how do I put a hot link inIn SwiftUI, you can create a hot link (a tappable link that opens a URL) using the Link view. Here's how you can do it:

Example

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Welcome to our app!")
                .font(.title)
                .padding()
            
            Link("Visit Apple", destination: URL(string: "https://www.apple.com")!)
                .font(.title2)
                .foregroundColor(.blue)
                .padding()
            
            Spacer()
        }
        .padding()
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation

  1. Link View:

    • The Link view takes two parameters: a string for the link text and a URL object for the destination.
    • The text in the Link view is styled like any other SwiftUI text view. You can customize it using modifiers like .font and .foregroundColor.
  2. Example Usage:

    • In the example, the Link is placed inside a VStack along with other UI elements.
    • When the user taps "Visit Apple," it will open the Apple website in the default web browser.

This approach is straightforward and adheres to SwiftUI's declarative syntax, allowing you to easily add clickable links to your views.

Display CreditsView on Button created at Tue Jan 20 1970 21:18:28 GMT+0000 (Coordinated Universal Time)

Related Chats

Display CreditsView on Button 0.999

Reusable SwiftUI custom view. 0.808

MultipeerManager Error Fix 0.805

Immersive View Phases 0.794

Preload Assets in visionOS 0.785

Switch to guard statement 0.775

SwiftUI RealityKit Integration 0.763

Handling App Exit 0.761

Animate Using withAnimation 0.760

Shared ViewModel Across Views 0.731