SwiftUI - Overview



SwiftUI is a framework which is used to develop responsive and creative UIs for all Apple's platforms such as macOS, iOS, watchOS, tvOS, visionOS, ipadOS, etc. It is lightweight and faster as compared to traditional frameworks such as UIKit.

It uses a declarative syntax to create a dynamic App. SwiftUI supports cross-platform means a single app can run on all Apple's platforms which saves the time and effort of the developers. Following are the features provided by SwiftUI −

  • Declarative Syntax

  • Live Previews

  • Layout

  • Views and Modifiers

  • Gestures

  • Animation

  • UI Controls

  • Shapes

  • Continuous Updates and Improvements

Declarative Syntax

SwiftUI uses declarative syntaxes to develop an App. Declarative syntaxes allow developers to describe the user interface and its behaviour and the rest of the implementation is done by the SwiftUI itself. This approach simplifies the code so that it would be easy to understand and reuse. It focuses on what should be displayed rather than how it is rendered.

Example

struct ContentView: View {
   var body: some View {
      Text("Hello")
   }
}

Live Previews

SwiftUI provide live previews in the Xcode. Whenever the developer makes any changes in the code the live preview will immediately display those changes on the screen. This feature is very helpful in improving errors, speeding up the development process, and improving debugging.

Layout

SwiftUI supports layout systems based on stacks such as VStack, HStack, and ZStack. This system simplifies the alignment of components in the current view. It can also allow nesting of views.

Example

struct ContentView: View {   
   var body: some View {
      VStack{
         Text("TutorialsPoint").font(.title)
      }
   }
}

Views and Modifiers

SwiftUI provides various in-built views such as Text, Image, Toggle, etc., and modifiers such as .font, .fill, .stroke, .foregroundColor, etc. Views are the fundamental building block of the interface whereas modifiers are used to change the appearance of the views. Using these views and modifiers we can create easy or complex user interfaces with few lines of code.

Example

struct ContentView: View {
   var body: some View {
      Text("TutorialsPoint").font(.title)
   }
}

Gesture

Gestures are used to add interactivity to the interface by responding to the user inputs like tap, long press, drag, hover, etc. SwiftUI provides some in-built gestures like Tap, Long Press, Drag, Rotation, etc., using these gestures we can create an interactive UI.

Example

struct ContentView: View {
   var body: some View {
      Text("TutorialsPoint").onTapGesture {
         // Handling tap gesture
      }
   }
}

Animation

In SwiftUI, we can animate any component very easily with the help of the .withAnimation() modifier. It also provides inbuilt animations and transitions which we can easily apply on the UI components. Animation enhances the user experience of the app and can improve the interactivity of the App. We can create custom animation very easily.

Example

struct ContentView: View {
   @State private var size: CGFloat = 2.0

   var body: some View {
      Button("Tap me to see the magic") {
         withAnimation {
            self.size *= 2.5
         }
      }.scaleEffect(size)
   }
}

UI Controls

SwiftUI supports various in-built UI controls such as buttons, sliders, text fields, pickers, etc. Controls are the most essential part of the UI using these controls developers can easily develop user-friendly apps for Apple's platforms.

Example

struct ContentView: View {
   var body: some View {
      Button(action:{
         // Hanldle button action
      })
   }
}

Shapes

Shapes are used to create graphic elements in the view. SwiftUI provides various in-built shapes such as Circle, Rectangle, Ellipse, Capsule, RoundedRectangle, etc. We can also create custom shapes with the help of the Shape protocol. We can also modify the appearance of the shapes with the help of various modifiers such as .frame, .fill, .strocke, etc.

Example

struct ContentView: View {
   var body: some View {
      Circle().fill(.red)
   }
}

Continuous Updates and Improvements

As we know SwiftUI is a new framework. So it is actively maintained and updated by Apple. In every update, they improve and add new features so that users can develop more user-friendly and interactive apps. They also fix bugs in each release.

Advertisements