SwiftUI and Jetpack Compose: Neck to Neck?

Chetansinh Rajput
Mobile Innovation Network
4 min readJul 11, 2024

If you’re an iOS developer familiar with SwiftUI or an Android developer using Jetpack Compose, you might be curious about how similar these modern UI frameworks are. As an iOS developer, when I started exploring Jetpack Compose, I found it much easier than I imagined. Jetpack Compose is so similar to SwiftUI that I adapted to it very quickly. So, I thought of starting a series on the similarities between these two frameworks. Today, let’s start with UI elements. We’ll explore other areas in upcoming blogs. This will make it easier for Android developers to adapt to SwiftUI and for iOS developers to adapt to Jetpack Compose. Let’s dive in!

Hello World

Ever wondered how to say “Hello World” in SwiftUI and Jetpack Compose?It’s not like learning two different languages. It’s more like saying hello in two different ways. Both are easy and straightforward.

Jetpack Compose:
In Jetpack Compose, you define a composable function to display text. A composable function is a building block of the UI, annotated with @Composable. Here's how you display "Hello World":

@Composable
fun HelloWorld() {
Text("Hello World!")
}

SwiftUI:
In SwiftUI, you achieve the same result by defining a body property within a view. This body property returns a view hierarchy. Here's the SwiftUI equivalent:

var body: some View {
Text("Hello World!")
}

Layout

Curious about arranging views in SwiftUI and Jetpack Compose?
Think of it like building with LEGO blocks instead of assembling complicated furniture. Both frameworks offer intuitive layout components that make arranging views easy and enjoyable.

In SwiftUI, you use VStack, HStack, and ZStack, which are equivalent to Column, Row, and Box in Jetpack Compose, respectively.

Jetpack Compose:
In Jetpack Compose, you can nest layout components to arrange views:

@Composable
fun NestedLayoutExample() {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Row(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Text("Inside Row")
}
Text("Inside Column")
}
Text("Inside ZStack")
}
}

SwiftUI:
In SwiftUI, the layout components work similarly:

struct NestedLayoutExample: View {
var body: some View {
ZStack {
VStack {
HStack {
Text("Inside HStack")
}
Text("Inside VStack")
}
Text("Inside ZStack")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
}

Modifiers

Wondering how to style elements in SwiftUI and Jetpack Compose?
It’s like accessorizing your outfit. Both frameworks let you add a variety of modifiers to customize your UI elements, giving you full control over their appearance and behavior.

Jetpack Compose:
In Jetpack Compose, you use modifiers to apply styling and behavior to your UI elements:

Text(
"Hello",
modifier = Modifier
.background(Color.Yellow)
.padding(8.dp)
.border(2.dp, Color.Black)
)

SwiftUI:
In SwiftUI, you chain modifiers to achieve similar effects:

Text("Hello")
.padding(8)
.background(Color.yellow)
.border(Color.black, width: 2)

Buttons

Ever tried creating a button in SwiftUI and Jetpack Compose?
It’s as simple as it gets! Both frameworks make it quick and easy to add interactive buttons to your app.

Jetpack Compose:

@Composable
fun ButtonExample() {
Button(onClick = { /* Do something */ }) {
Text("Click Me")
}
}

SwiftUI:

struct ButtonExample: View {
var body: some View {
Button(action: {
// Do something
}) {
Text("Click Me")
}
}
}

Text Fields

Handling text input in SwiftUI and Jetpack Compose?
It’s no mystery! Both frameworks make it easy and intuitive to manage user input.

Jetpack Compose:

@Composable
fun TextFieldExample() {
var text by remember { mutableStateOf("") }

TextField(
value = text,
onValueChange = { text = it },
label = { Text("Enter text") }
)
}

SwiftUI:

struct TextFieldExample: View {
@State private var text: String = ""

var body: some View {
TextField("Enter text", text: $text)
.padding()
.border(Color.gray)
}
}

Switch (Toggle)

Creating a switch in SwiftUI and Jetpack Compose?
It’s as simple as flipping a switch!

Jetpack Compose:
In Jetpack Compose, you can create a switch with minimal code:

@Composable
fun SwitchExample() {
var checked by remember { mutableStateOf(false) }

Switch(
checked = checked,
onCheckedChange = { checked = it }
)
}

SwiftUI:
In SwiftUI, you achieve the same functionality with the Toggle component:

struct ToggleExample: View {
@State private var isOn: Bool = false

var body: some View {
Toggle("Switch me", isOn: $isOn)
.padding()
}
}

Progress Bar

Showing a progress bar in SwiftUI and Jetpack Compose?
It’s as straightforward as taking a relaxing walk.

Jetpack Compose:
In Jetpack Compose, displaying a circular progress indicator is simple:

@Composable
fun ProgressBarExample() {
CircularProgressIndicator()
}

SwiftUI:
In SwiftUI, you can show a progress view with circular style:

struct ProgressViewExample: View {
var body: some View {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.padding()
}
}

Images

Displaying images in SwiftUI and Jetpack Compose?
It’s as effortless as capturing a snapshot!

Jetpack Compose:
In Jetpack Compose, you can easily display an image:

@Composable
fun ImageExample() {
Image(
painter = painterResource(R.drawable.ic_launcher_foreground),
contentDescription = "Example Image"
)
}

SwiftUI:
SwiftUI offers a similar straightforward approach for displaying images, typically using the Image view with system or custom images:

struct ImageExample: View {
var body: some View {
Image(systemName: "star.fill")
.resizable()
.frame(width: 50, height: 50)
.padding()
}
}

Conclusion

As you can see, SwiftUI and Jetpack Compose have many similarities, making it easier for developers to transition between iOS and Android development. By understanding the equivalents of common UI elements and patterns, you can leverage your knowledge across both platforms and create beautiful, modern applications. Stay tuned for the next part of this series, where we’ll explore more areas of these frameworks and continue to make cross-platform development a breeze.

Happy coding!

Connect with us 👇

Linkedin

GitHub

--

--