Why you shouldn’t use SwiftUI (currently)
SwiftUI is Apple’s attempt at providing declarative UI for iOS and Mac developers. During WWDC 2020, Apple has announced new extensions to the SwiftUI. When SwiftUI was revealed to the world last year, it had many limitations. Has the situation improved?
In my short-lived attempt to investigate improved SwiftUI capabilities, I found out that my biggest issue was the lack of the UIKit’s APIs. This was apparent in many places. The first place was when I tried to add a custom gesture recognizer to a view in SwiftUI. In theory, you can bridge gesture recognizer from UIKit and use it in SwiftUI. However, when I tried to do that, SwiftUI generated a new view in a view hierarchy with my custom gesture recognizer instead of adding it to the existing view. That’s not what I wanted.
Another problem I found was layouting. SwiftUI is supposed to make layouting easier. You can set the
frame modifier on every view to set the size, but as soon as you want to pair with some other modifier, e.g.,
scaleEffect to scale up a specific view, it turns out that the order of these modifiers matters. I had to use
frame to get it working. What is the reason for this?
One more major problem with layouting I had was scaling a view. I wanted a view to be anchored to the left side of the screen and to expand itself to the right side. This turned out to be problematic. My view was always centered horizontally and was expanded equally towards the edges of the screen. Doing this with constraints wouldn’t be an issue at all, but they are not available in the SwiftUI. The replacement system available is unfortunately limited.
Overall I think that SwiftUI has potential, but it’s not there yet despite Apple’s claims. Lack of the APIs existing in the UIKit is a major problem, and so is its quirkiness in some areas. Another issue to point out is its availability. You have to use iOS 13 or later.
Hopefully, SwiftUI will keep getting better to the point where it is a viable option for apps. In its current state, I cannot recommend it. While it might be enough for simple widgets in iOS 14, it is very limiting compared to the UIKit. For now, we will have to keep living with the UIKit, and its constraints as they aren’t going away any time soon, in my opinion.
Feedback is welcome!Want to join the discussion?
Feel free to contribute!