Do you use UIKit or SwiftUI in your projects?
sobes.tech AI
Answer from AI
I use both UIKit and SwiftUI. The choice depends on the project requirements:
-
UIKit is preferred for projects with strict compatibility requirements with older iOS versions, complex customization of elements, integration with legacy code, or specific navigation patterns that SwiftUI does not fully support natively yet. UIKit is also often more convenient for creating custom controls with non-standard behavior or deep integration with Core Animation.
-
SwiftUI is used for new projects that require rapid development, a declarative approach to building interfaces, easy state management, and real-time previews. It is ideal for cross-platform development within the Apple ecosystem (iOS, macOS, watchOS, tvOS). It is well-suited for creating complex animations and transitions with minimal code. Working with data through
@State,@Binding,@ObservedObject,@EnvironmentObject, and@FetchRequestgreatly simplifies view logic.
I often combine both frameworks (interoperability) by using UIHostingController to embed SwiftUI views into UIKit or UIViewRepresentable/UIViewControllerRepresentable to use UIKit components within SwiftUI.
Example of embedding a SwiftUI View into a UIKit ViewController:
import SwiftUI
import UIKit
class MyUIKitViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swiftUIView = MySwiftUIView() // Your SwiftUI View
let hostingController = UIHostingController(rootView: swiftUIView)
addChild(hostingController)
view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
}
struct MySwiftUIView: View {
var body: some View {
Text("Hello from SwiftUI!")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}