Build class-apart UI on apple devices using SwiftUI

Developing exceptional UI using Declarative UI in SwiftUI

Want mind-blowing user interfaces on premium apple devices? Looking for an option that is easy to use and learn? SwiftUI is your solution. SwiftUI is the simplest way one can build user interfaces across all platforms.  SwiftUI was launched at the Worldwide Developer Conference (WWDC) 2019. Soon the developers began to leverage the published APIs that helped them adopt newly introduced SwiftUI features efficiently.

In WWDC 2020, Apple unveiled a few more APIs, which will help develop complete apps using SwiftUI without adding any UIApplicationDelegate or SceneDelegate to the project.

Earlier in iOS 13, the developers would write the code below in SceneDelegate.swift file to attach SwiftUI View to the app:

After introducing XCode 12 and iOS 14, developers agreed on removing AppDelegate and SceneDelegate from apps.

Here is the latest Project Template Structure of XCode 12,

Now, you can define and attach your app using the  code mentioned below in MySwiftUIApp.swift file:

So, ContentView will be displayed to the user once the app launches.

Let’s start adding some Text to the ContentView.swift file:

Its preview will look like below:

SwiftUI also allows you to modify text and other information and cons by writing the code below; we will make separate strings for each view to make the code look more organized, readable, and testable.

This will give an output as below:

We can also add an image as a background. See below:

The output will be the same as:

In conclusion, one can build apps using SwiftUI easily.

Here is a comparison chart between the features of UIKit to SwiftUI; this will make it easier for you to determine various resources and identify which amongst the two matches your requirements.

UIKitSwiftUINote
UIViewControllerView
UITableViewControllerListAlternatively we can configure LazyHStack & LazyVStack with ScrollView
UICollectionViewControllerUICollectionViewControllerIntroduced in iOS 14.
UISplitViewControllerNavigationView
UINavigationControllerNavigationView
UIPageViewControllerTabViewIntroduced in iOS 14 as style of TabView
UITabBarControllerTabView
UIAlertControllerAlert
UILabel TextLabel
UITabBarTabView
UITabBarItemTabViewAvailable as .tabItem with TabView
UITextFieldTextFieldSecureField is available for password – secure entry
UITextViewTextEditorThis feature is introduced from iOS 14
UITableViewList
UINavigationBarNavigationView
UINavigationItemToolbarItemThis feature is introduced from iOS 14
UIBarButtonItemNavigationView
UICollectionViewLazyVGrid and LazyHGridThis feature is introduced from iOS 14
UIStackViewHStackLazyHStack
UIStackViewVStackLazyVStack
UIScrollViewScrollView
UIActivityIndicatorViewProgressView with CircularProgressViewStyleThis feature is introduced from iOS 14
UIImageViewImage
UIPickerViewPicker
UIButtonButtonLink
UIDatePickerDatePicker
UIPageControlThis feature is introduced from iOS 14. Available as PageTabViewStyle style of TabView. Also with modifier named .indexViewStyle.
UIProgressViewProgressViewThis feature is introduced from iOS 14
UISegmentedControlPickerAvailable as SegmentedPickerStyle of new Picker of SwiftUI
UISliderSlider
UIStepperStepper
UISwitchToggle
UIToolBarNavigationView with .toolbarThis feature is introduced from iOS 14
MKMapViewMapimport MKMapView to use default UIKit Map View

Pros of SwiftUI:

  • Easy to adapt, cleaner, and simpler code.
  • Can be clubbed with UIKit using UIHostingController and UIViewRepresentable.
  • Easier theme management. Switching between themes is less complicated.
  • SwiftUI comes with the power of two-way binding with BindableObject, ObjectBinding, and the combined framework.
  • Live preview is accurate. With an increasing demand for cross platforms and advanced platforms, it is a much-needed feature to attract developers.
  • SwiftUI does not require Storyboard/Interface Builder. Canvas replaces it.
  • SwiftUI does not require Storyboard/Interface Builder. Canvas replaces it.

Cons of SwiftUI:

  • Support starts from iOS 13 and Xcode 11. No backward compatibility. Also, some of the components introduced in iOS 14 may not work with the iOS 13 version.
  • No or less forum or developer support, less equipped conflict handling will create weak architectural issues.

Are you thorough with how you can use declarative UI to create a mobile application by leveraging SwiftUI? If you have any queries, get in touch with an expert at DEV IT now.

The following two tabs change content below.

Milan Manwar

Senior Software Developer (Mobile) at Dev Information Technology Ltd.
I'm a passionate programmer who is always keen to learn new technologies and latest technology trends. I always prefer to explore new technologies and experiment on its applications and to share the same with people around me.

Leave a Reply

Your email address will not be published. Required fields are marked *