Do you use UIKit or SwiftUI in your work?
sobes.tech AI
Answer from AI
Yes, I use both frameworks depending on the project requirements.
UIKit is a mature framework suitable for projects with a long history, complex customization of interface elements, integration with low-level iOS components, and cases where maximum backward compatibility is needed.
SwiftUI is a declarative approach that simplifies interface development, especially for new projects, cross-platform applications (iOS, macOS, watchOS, tvOS), and prototyping. It allows writing more concise and understandable code.
Projects often use a combination of these frameworks, for example, using UIHostingController to embed SwiftUI views into UIKit and vice versa.
When choosing a framework, I consider the following factors:
- Support for older OS versions: UIKit is better for older iOS versions.
- UI/Animation complexity: SwiftUI often simplifies creating complex animations and transitions.
- Team size and experience: Knowledge of UIKit is more widespread in the market.
- Future development of the project: SwiftUI is actively developing and is the future native technology of Apple.
// Example of using UIHostingController for a SwiftUI View in UIKit
import UIKit
import SwiftUI
class MyUIKitViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mySwiftUIView = MyCustomSwiftUIView()
let hostingController = UIHostingController(rootView: mySwiftUIView)
// Adding SwiftUI view to the UIViewController hierarchy
addChild(hostingController)
view.addSubview(hostingController.view)
// Setting Constraints
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
hostingController.didMove(toParent: self)
}
}
struct MyCustomSwiftUIView: View {
var body: some View {
Text("Hello from SwiftUI!")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
Both frameworks have their strengths, and the choice depends on the specific task and project context.