Skip to content
Draft
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
8 changes: 6 additions & 2 deletions Development/Development.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
4B7DF0652BEA8D7A00C59107 /* BookHosting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7DF0642BEA8D7900C59107 /* BookHosting.swift */; };
4B7E762429B9A1EF009DE246 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7E762329B9A1EF009DE246 /* ContentView.swift */; };
4B7E762629B9A1F0009DE246 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4B7E762529B9A1F0009DE246 /* Assets.xcassets */; };
4B7E762929B9A1F0009DE246 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4B7E762829B9A1F0009DE246 /* Preview Assets.xcassets */; };
Expand All @@ -18,6 +19,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
4B7DF0642BEA8D7900C59107 /* BookHosting.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BookHosting.swift; sourceTree = "<group>"; };
4B7E761E29B9A1EF009DE246 /* Development.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Development.app; sourceTree = BUILT_PRODUCTS_DIR; };
4B7E762329B9A1EF009DE246 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
4B7E762529B9A1F0009DE246 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -87,6 +89,7 @@
isa = PBXGroup;
children = (
4B7E763729B9B105009DE246 /* BookSizing.swift */,
4B7DF0642BEA8D7900C59107 /* BookHosting.swift */,
);
path = Book;
sourceTree = "<group>";
Expand Down Expand Up @@ -174,6 +177,7 @@
files = (
4B7E763029B9A214009DE246 /* AppDelegate.swift in Sources */,
4B7E762429B9A1EF009DE246 /* ContentView.swift in Sources */,
4B7DF0652BEA8D7A00C59107 /* BookHosting.swift in Sources */,
4B7E763829B9B105009DE246 /* BookSizing.swift in Sources */,
4B7E763B29B9B143009DE246 /* Util.swift in Sources */,
);
Expand Down Expand Up @@ -312,7 +316,7 @@
INFOPLIST_KEY_UILaunchStoryboardName = "Launch Screen.storyboard";
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -342,7 +346,7 @@
INFOPLIST_KEY_UILaunchStoryboardName = "Launch Screen.storyboard";
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
53 changes: 53 additions & 0 deletions Development/Development/Book/BookHosting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import SwiftUI
import SwiftUIHosting

@available(iOS 14.0, *)
struct BookHosting: View, PreviewProvider {
var body: some View {
ContentView()
}

static var previews: some View {
Self()
}

private struct ContentView: View {

@State var text: String = ""

var body: some View {
HostingViewHost {
VStack {
Color.purple.frame(width: 100, height: 100)
TextField("Text", text: $text)
Color.purple.frame(width: 100, height: 100)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea(.keyboard)
.background(Color(white: 0.1))
}
// .ignoresSafeArea()
// .background(Color(white: 0.1))
}
}
}

private struct HostingViewHost<Content: View>: UIViewRepresentable {

let content: () -> Content

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

func makeUIView(context: Context) -> UIView {
// _UIHostingView(rootView: AnyView(content()))
SwiftUIHostingView {
content()
}
}

func updateUIView(_ uiView: UIView, context: Context) {
// Nothing
}
}
20 changes: 13 additions & 7 deletions Development/Development/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import SwiftUI

struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Content") {
BookSizing()
}
}
}
// NavigationView {
// List {
// NavigationLink("Content") {
// BookSizing()
// }
// if #available(iOS 14.0, *) {
// NavigationLink("Hosting") {
// BookHosting()
// }
// }
// }
// }
BookHosting()
}
}

Expand Down
10 changes: 10 additions & 0 deletions Development/Development/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import SwiftUIHosting

func measureSize(content: some View, targetSize: CGSize) {

do {
if #available(iOS 15.0, *) {
let a = _makeUIHostingController(AnyView(content), tracksContentSize: true, secure: false)
print(a)
} else {
// Fallback on earlier versions
}

}

do {
let size = UIHostingController(rootView: content).sizeThatFits(in: targetSize)
print("System SizeThatFits:", size)
Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftUIHosting/HostingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ final class HostingController<Content: View>: UIHostingController<Content> {
#else
_disableSafeArea = disableSafeArea
#endif

if true {
guard let viewClass = object_getClass(view) else { return }

let viewSubclassName = String(cString: class_getName(viewClass)).appending("_IgnoresKeyboard")
if let viewSubclass = NSClassFromString(viewSubclassName) {
object_setClass(view, viewSubclass)
}
else {
guard let viewClassNameUtf8 = (viewSubclassName as NSString).utf8String else { return }
guard let viewSubclass = objc_allocateClassPair(viewClass, viewClassNameUtf8, 0) else { return }

if let method = class_getInstanceMethod(viewClass, NSSelectorFromString("keyboardWillShowWithNotification:")) {
let keyboardWillShow: @convention(block) (AnyObject, AnyObject) -> Void = { _, _ in }
class_addMethod(viewSubclass, NSSelectorFromString("keyboardWillShowWithNotification:"),
imp_implementationWithBlock(keyboardWillShow), method_getTypeEncoding(method))
}
objc_registerClassPair(viewSubclass)
object_setClass(view, viewSubclass)
}
}

}

@MainActor required dynamic init?(coder aDecoder: NSCoder) {
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftUIHosting/SwiftUIHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ open class SwiftUIHostingView: UIView {
}
}

private let hostingController: HostingController<AnyView>
private let hostingController: UIHostingController<AnyView>

public let configuration: Configuration

Expand Down Expand Up @@ -93,10 +93,10 @@ open class SwiftUIHostingView: UIView {
hostingController.view.leftAnchor.constraint(equalTo: leftAnchor),
])

hostingController.onViewDidLayoutSubviews = { controller in
// TODO: Reduces number of calling invalidation, it's going to be happen even it's same value.
controller.view.invalidateIntrinsicContentSize()
}
// hostingController.onViewDidLayoutSubviews = { controller in
// // TODO: Reduces number of calling invalidation, it's going to be happen even it's same value.
// controller.view.invalidateIntrinsicContentSize()
// }
}

@available(*, unavailable)
Expand Down