How do views work in SwiftUI
Insert SwiftUI views into existing UIKit applications
Is it possible to create views with SwiftUI alongside an existing UIKit application?
I wrote an existing application in Objective-C. I started migrating to Swift 5. I was wondering if I can use SwiftUI alongside my existing UIKit .xib views.
That said, I want some views made with SwiftUI and some other views made with UIKit in the same app. Of course, don't mix the two.
Working side by side
edit 05/06/19: Added information about UIHostingController as suggested by @Departamento B in his answer. Credits go to him!
Using SwiftUI in UIKit
Components can be used in existing environments by enclosing a in one of the following:
It is also possible to overwrite it and adapt it to your own needs, e.g. B. by manual adjustment if it does not work as expected.
is documented here.
Using UIKit in SwiftUI
If an existing view is to be used in an environment, the log will help! It's documented here and can be seen in action in this official Apple tutorial.
Please note that you cannot use it with iOS versions If you want to use it in a project with a goal on iOS 13, you need to add attributes to your structures.
If you want to use it in a project with a goal on iOS 13, you need to add attributes to your structures.
While the documentation for the class hasn't been written yet, it seems to be exactly what you're looking for: https://developer.apple.com/documentation/swiftui/uihostingcontroller
I just tried the following line of code in my app:
Where is only the default "Hello World" from. This works exactly as you'd expect. It works even if you are subclass.
If you want to embed SwiftUI in a UIKit View Controller, use a Container View.
One item that I haven't mentioned yet that involves Xcode 11 Beta 5 (11M382q) involves updating your app's info.plist file.
In my scenario, I take an existing Swift & UIKit-based application and completely migrate it to an iOS 13 & pure SwiftUI app, so backward compatibility is not a problem for me.
After making the necessary changes to AppDelegate:
And adding a SceneDelegate class:
I had a problem where my SceneDelegate would not be called. This was fixed by adding the following to my info.plist file:
And to see a screenshot:
The most important elements that need to be kept in sync are:
- Delegate the class name, so that Xcode knows where your file is
- Configuration name, so that the call in AppDelegate can load the correct one
After that I was able to load my newly created HomeList view (a SwiftUI object).
If you're looking to create a view from an older Objective C project, this technique worked perfectly for me.
See Adding SwiftUI to Objective-C Apps
Kudos to our friend who wrote this.
You can use the component in the Interface Builder:
Then when you have a simple one like this:
You can set it as the controller's custom class:
And then you can use it like a normal one
Don't forget to import wherever you need it
If you are faced with layout issues, you need to add restrictions to the view of the UIHostingController.
with this extension:
You can use them together. You can 'transfer' one to order through compliance. See the official tutorial for details.
However, you should also consider compatibility.
Here is the code snippet from the log from SwiftUI :
So it is not backwards compatible.
- iOS 13.0+
- macOS 10.15+
- watchOS 6.0+
Others have shown how UIHostingController is used .
I can show you how to use a UIViewController from SwiftUI UIViewControllerRepresentable can present :
- Think freckles are attractive
- What is the best treatment for Soriosis
- India is self-sufficient in Essen
- How do I get a free VPN
- What is the growing season for tomatoes
- Fear creates an insatiable need for food
- What is Hillary Clinton doing now
- Is used correctly in the sentence
- How does a game company work
- What is the temporary hard water
- Created the Joker Two Face
- Leaving tech company California
- What is the melting point of oxygen
- Pentesting a sought-after skill
- Why did Romania enter World War I.
- Which industries are best at Coimbatore
- What is the nature of Indian philosophy
- Why are there escapes
- Why do we extract DNA from tissue
- In gender-specific languages, society is feminine
- What kind of car is Tata Tiago
- Why should a man have sex
- Gifted children talk late
- Are phobias physiological or psychological