跳转至
Class

UIHostingController

A UIKit view controller that manages a SwiftUI view hierarchy.

Declaration

class UIHostingController<Content> : UIViewController where Content : View

Overview

Create a UIHostingController object when you want to integrate SwiftUI views into a UIKit view hierarchy. At creation time, specify the SwiftUI view you want to use as the root view for this view controller; you can change that view later using the rootView property. Use the hosting controller like you would any other view controller, by presenting it or embedding it as a child view controller in your interface.

Availability

iOS 13.0+

tvOS 13.0+

Topics


Initializer

init(coder:) Creates a hosting controller object from the contents of the specified archive.

init(coder:rootView:) Creates a hosting controller object from an archive and the specified SwiftUI view.

init(nibName:bundle:) Returns a newly initialized view controller with the nib file in the specified bundle.

init(rootView:) Creates a hosting controller object that wraps the specified SwiftUI view.


Instance Property

childForStatusBarHidden Called when the system needs the view controller to use for determining status bar hidden/unhidden state.

keyCommands The key commands that trigger actions on this responder.

preferredStatusBarStyle The preferred status bar style for the view controller.

preferredStatusBarUpdateAnimation The animation style to use when hiding or showing the status bar for this view controller.

prefersStatusBarHidden A Boolean value that indicates whether the view controller prefers the status bar to be hidden or shown.

rootView The root view of the SwiftUI view hierarchy managed by this view controller.


Instance Method

didMove(toParent:) Called after the hosting controller is added or removed from a container view controller.

sizeThatFits(in:) Calculates and returns the most appropriate size for the current view.

target(forAction:withSender:) Returns the target object that responds to an action.

viewDidAppear(_:) Notifies the view controller that its view has been added to a view hierarchy.

viewWillAppear(_:) Notifies the view controller that its view is about to be added to a view hierarchy.

viewWillDisappear(_:) Notifies the view controller that its view will be removed from a view hierarchy.

viewWillLayoutSubviews() Called to notify the view controller that its view is about to layout its subviews.

viewWillTransition(to:with:) Notifies the container that the size of its view is about to change.

willMove(toParent:) Called just before the hosting controller is added or removed from a container view controller.