Challenges of mixed SwiftUI and UIKit codebases in iOS Apps

Imagine you are a software engineer, or you really are. You create iOS apps on a daily basis. You use only the latest and finest trends in mobile development, such as SPM, Swift Data, and, of course, SwiftUI. Life couldn’t be better. But reality is cruel. And in real life you probably need to maintain and develop an application that was written several years ago when these technologies didn’t exist at all.

Due to your natural curiosity and desire to build top-notch products, you still strive to integrate the best approaches in your app. The Medium iOS team is not an exception. And given that our codebase dates back to 2013, it has a bunch of UIKit views.

In this post, I’d like to cover some differences SwiftUI and UIKit interactions, pitfalls, and things to consider before making the decision to upgrade your views from UIKit to SwiftUI.

  1. Integration Complexity & Code Duplication

Integrating SwiftUI views into a UIKit project might not be as seamless as anticipated. SwiftUI uses a different approach with its declarative syntax and layout system compared to UIKit’s imperative approach. SwiftUI views can be embedded within UIKit using UIHostingController. One of the most common problems we faced is sizing, especially for cells (showing SwiftUI view as UITableViewCell or UICollectionViewCell when they don’t have fixed dimensions).

When it comes to scroll tracking, SwiftUI and UIKit have different API and approaches for this. The differences start even in details like scroll offset. In UIKit, it has a negative value, while in SwiftUI, it is positive.

To solve the abovementioned issue about cells sizing, we sometimes have to implement the same view twice — once in SwiftUI, and once in UIKit. Given that we try to use reusable design components, this adds complexity during development and even more when we need to make changes in these components.

This becomes even more serious when we talk about things like PostPreview, which is an essential component of the Medium app.

ホーム - Wiki
Copyright © 2011-2024 iteam. Current version is 2.139.0. UTC+08:00, 2024-12-27 00:04
浙ICP备14020137号-1 $お客様$