Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Revert "Run body closure inside SwiftUI.View.body to activate observa…
…tion tra…"

This reverts commit 368cf35.
  • Loading branch information
muukii authored Sep 25, 2025
commit ed0c2bea80e735c0a948bf8c9dcf2a2c383b18f6
27 changes: 5 additions & 22 deletions Sources/SwiftUIHosting/SwiftUIHostingView.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import SwiftUI

private struct Wrapper<Content: View>: View {

let content: () -> Content

init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

var body: some View {
content()
}

}

/// A view that hosts SwiftUI for UIKit environment.
open class SwiftUIHostingView<Content: View>: UIView {

private let hostingController: HostingController<Wrapper<ModifiedContent<Content, BaseModifier>>>
private let hostingController: HostingController<ModifiedContent<Content, BaseModifier>>

public let configuration: SwiftUIHostingConfiguration

Expand All @@ -27,16 +13,13 @@ open class SwiftUIHostingView<Content: View>: UIView {
_ function: StaticString = #function,
_ line: UInt = #line,
configuration: SwiftUIHostingConfiguration = .init(),
@ViewBuilder content: @escaping @MainActor () -> Content
@ViewBuilder content: @MainActor () -> Content
) {

self.configuration = configuration

let usingContent = Wrapper {
// Let SwiftUI Observation works
content().modifier(configuration.baseModifier)
}

let usingContent = content().modifier(configuration.baseModifier)

#if DEBUG

self.hostingController = HostingController(
Expand Down Expand Up @@ -199,7 +182,7 @@ open class AnySwiftUIHostingView: SwiftUIHostingView<AnyView> {
_ function: StaticString = #function,
_ line: UInt = #line,
configuration: SwiftUIHostingConfiguration = .init(),
@ViewBuilder content: @escaping @MainActor () -> AnyViewContent
@ViewBuilder content: @MainActor () -> AnyViewContent
) {
super.init(
name,
Expand Down
10 changes: 3 additions & 7 deletions Sources/SwiftUIHosting/SwiftUIHostingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ open class SwiftUIHostingViewController<Content: View>: UIViewController {

super.viewDidLoad()

let _content = content(self)

let contentView = SwiftUIHostingView(
name,
file,
function,
line,
configuration: configuration
) { [weak self] in
if let self {
content(self)
} else {
EmptyView()
}
}
) { _content }

view.addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
Expand Down