How do you make a bottom sheet just like in Apple Maps or Find My using UIKit, but using it from SwiftUI?

Generated on 8/2/2024

1 search

To create a bottom sheet similar to those in Apple Maps or Find My using UIKit but from SwiftUI, you can leverage the interoperability between SwiftUI and UIKit. Specifically, you can use UIViewControllerRepresentable to wrap a UIKit view controller and present it as a SwiftUI view.

Here's a step-by-step guide:

  1. Create a UIKit View Controller for the Bottom Sheet: Define a custom UIViewController that will act as your bottom sheet. You can use UISheetPresentationController to manage the sheet's presentation.

  2. Wrap the UIKit View Controller in SwiftUI: Use UIViewControllerRepresentable to create a SwiftUI-compatible wrapper for your custom view controller.

  3. Present the Bottom Sheet in SwiftUI: Use SwiftUI's .sheet modifier to present the wrapped view controller as a bottom sheet.

Example Code

Step 1: Create a UIKit View Controller

import UIKit

class BottomSheetViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // Add your custom UI elements here
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let sheet = self.sheetPresentationController {
            sheet.detents = [.medium(), .large()]
            sheet.prefersGrabberVisible = true
        }
    }
}

Step 2: Wrap the UIKit View Controller

import SwiftUI

struct BottomSheetView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> BottomSheetViewController {
        return BottomSheetViewController()
    }
    
    func updateUIViewController(_ uiViewController: BottomSheetViewController, context: Context) {
        // Update the view controller if needed
    }
}

Step 3: Present the Bottom Sheet in SwiftUI

import SwiftUI

struct ContentView: View {
    @State private var showSheet = false
    
    var body: some View {
        VStack {
            Button("Show Bottom Sheet") {
                showSheet.toggle()
            }
        }
        .sheet(isPresented: $showSheet) {
            BottomSheetView()
        }
    }
}

Relevant Sessions

For more details on integrating UIKit with SwiftUI, you can refer to the session SwiftUI essentials which covers SDK interoperability and how to use UIViewControllerRepresentable.

Additional Resources

These sessions provide insights into using UIKit components within SwiftUI and enhancing UI animations and transitions, which can be useful for creating a polished bottom sheet experience.

Migrate your TVML app to SwiftUI

Migrate your TVML app to SwiftUI

SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

Evolve your document launch experience

Evolve your document launch experience

Make your document-based app stand out, and bring its unique identity into focus with the new document launch experience. Learn how to leverage the new API to customize the first screen people see when they launch your app. Utilize the new system-provided design, and amend it with custom actions, delightful decorative views, and impressive animations.

SwiftUI essentials

SwiftUI essentials

Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Enhance your UI animations and transitions

Enhance your UI animations and transitions

Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.

Unlock the power of places with MapKit

Unlock the power of places with MapKit

Discover powerful new ways to integrate maps into your apps and websites with MapKit and MapKit JS. Learn how to save and reference unique places using Place ID. Check out improvements to search that make it more efficient to find relevant places. Get introduced to the new Place Card API that lets you display rich information about places so customers can explore destinations right in your app. And, we’ll show you quick ways to embed maps in your website with our simplified token provisioning and Web Embed API.

Get started with HealthKit in visionOS

Get started with HealthKit in visionOS

Discover how to use HealthKit to create experiences that take full advantage of the spatial canvas. Learn the capabilities of HealthKit on the platform, find out how to bring an existing iPadOS app to visionOS, and explore the special considerations governing HealthKit during a Guest User session. You’ll also learn ways to use SwiftUI, Swift Charts, and Swift concurrency to craft innovative experiences with HealthKit.