Categories
iOS Swift tvOS

My first SwiftUI view

Swift UI has been around for almost 2 years now, and I didn’t have much time / opportunity to play with it. But yesterday I did my first Swift UI view. This is not very advanced, as I wanted to replace a simple table view which shows text on each cell, and handles a click on them. Doing this in UIKit is very fast for me as I have done this hundred of time, but it took me quite some time to handle in Swift UI because it’s all new. New but exciting!!!!

What I wrote

 import SwiftUI
 
 struct DemoListView: View {
     
     let items: [DemoItem]
     var router: Router?
     
     var body: some View {
         
         List {
             ForEach(items, id: \.self) { item in
                 HStack {
                     Button(action: {
                         router?.navigate(item)
                     }, label: {
                         Text(item.title)
                     })
                     Spacer()
                 }
                 .contentShape(Rectangle())
             }
             Spacer()
         }
         .navigationBarTitle("Demo - SwiftUI")
     }
 }


 struct DemoListView_Previews: PreviewProvider {
     static var previews: some View {
         DemoListView(items: DemoItem.allCases, router: nil)
     }
 } 

How I feel about SwiftUI

✅ Pros

  • I was able to run this SwifUI view within a complete UIKit application, by wrapping the View into a UIHostingController. So current UI implementation could be replace gradually piece by piece using SwiftUI, starting with small views.
  • Same Swift UI code was running on the tvOS version of my application.
  • The rendering looks “almost-exactly” the same as a UIKit TableView (in fact, it uses UIKit behind the scene).
  • Code seems more concise than with UIKit code.
  • Preview in XCode is great, as it give very quick feedback on the results without need to trigger the whole application.
  • So far positive, and even after 1 evening, I was able to manage this list of items.

🔥 Cons (for now)

  • Preview stopped working at some points, and I got some strange error. Since then I’m unable to run the preview, I’ve not idea what’s wrong yet.
 ConfigurationError: noPreviewInfos(arch: "x86_64", sdkRoot: "/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator14.3.sdk") 

  • To so some very simple thing, I have to use Google and the swift UI documentation. It will take some time to learn all the various things that are needed to know in order to make some advanced UI.
  • I wonder how very complex UI with Carousels, grids, lot of buttons and interactions will be working well with code and with the @State or @ObservedObject to manage the data.
  • Swift UI is only iOS 13/14, so it’s not possible to use for supporting older version of the operating system. Therefore UIKit is still pretty much there and needed.

Thank you for reading until here…  👊 Have a good week end…  🍺