SwiftUI: Exploring the NavigationView View

The NavigationView view is a crucial component in SwiftUI that you will frequently use in your app development. By wrapping your views within a NavigationView, you gain access to several beneficial features. Let’s start by taking a look at a basic NavigationView: NavigationView { } Once you have enclosed a view within a NavigationView, you can easily add a title to the view using the navigationTitle() modifier: NavigationView { Text("Hello") ....

SwiftUI: Formatting Decimals in Text View

When using the Slider view to select a value in SwiftUI, we often encounter an issue when displaying the value in a Text view. By default, the value is displayed as a decimal, even when a step value of 1 is used. For example, the number 34 appears as 34.000000. To format the value and display it without the decimal places, we can utilize the specifier parameter when interpolating the value in the Text view....

SwiftUI: How to Create a Tab View

Tab views are a common UI pattern in iOS apps, allowing users to navigate between different sections of an app by tapping icons or labels at the bottom of the screen. In SwiftUI, creating a tab view is made simple with the TabView view. Here is the basic implementation of a TabView: import SwiftUI struct ContentView: View { var body: some View { TabView { Text("First") .tabItem { Label("First", systemImage: "tray") } Text("Second") ....

SwiftUI: Mastering Stacks and Groups

In SwiftUI, creating an app usually involves more than just a single view, such as a basic “Hello World” app. To add multiple views, you need to use stacks. There are three types of stacks in SwiftUI: HStack, VStack, and ZStack. The Hello World app initially looks like this: import SwiftUI struct ContentView: View { var body: some View { Text("Hello World") } } If you try to add a second Text view directly within the ContentView struct, like this:...

SwiftUI: Properties

In SwiftUI, you have the flexibility to add properties to any view to enhance its functionality. Let’s explore how properties can be used in SwiftUI views. import SwiftUI struct ContentView: View { let name = "Flavio" var body: some View { Text("Hello, \(name)!") .font(.largeTitle) } } In the above code snippet, we declare a constant property name of type String and assign it the value “Flavio”. This property is then used within the Text view to display a personalized greeting....

SwiftUI: Spacing

In the previous tutorial about SwiftUI, we discussed how views can be organized using stacks. Now, let’s dive into the topic of spacing. When using a VStack, you might notice that there is no space between the Text views by default. This is the default behavior of a VStack. However, you can adjust the spacing between the views by providing a spacing parameter: VStack(spacing: 100) { Text("Hello World") Text("Hello again!") } In this example, we set the spacing to 100 points, resulting in a 100-point gap between the views within the VStack....

SwiftUI: The ForEach View - Simplifying Iteration in SwiftUI

The ForEach view is a powerful tool in SwiftUI that allows us to easily iterate over an array or a range and generate views based on the iteration. It simplifies the process of creating repetitive views, such as lists or grids, by reducing the code required. Let’s start with a basic example. We can use ForEach to generate three Text views that display the numbers from 0 to 2: ForEach(0..<3) { number in Text("\(number)") } In this example, we use the ....

SwiftUI: Using Alerts to Display Messages

Alert messages are a useful way to provide feedback to users and aid in debugging applications. In SwiftUI, you can leverage the .alert() modifier to display alerts based on certain conditions. Let’s consider an example where we have a Button with an associated counter: import SwiftUI struct ContentView: View { @State var count = 0 var body: some View { Button("Count: \(count)") { self.count += 1 } .font(.title) } } Suppose we want to show an alert message when the count reaches 10....

SwiftUI: Using the Button View to Update App State

The Button view in SwiftUI allows you to create interactive button elements in your app. There are two ways to declare a Button: Button with a label: Button("Button label") { // Action to perform when button is tapped } Button with a closure label: Button { // Action to perform when button is tapped } label: { // Something else as the label, e.g., an image Text("Button label") } The second way is commonly used when the button has a label other than plain text, such as an image....

Syntax Highlighting for Any Web Page Element

When I needed to add syntax highlighting to a web page, I encountered the challenge of not being able to change the page’s markup. Many syntax highlighting libraries, such as Prism.js, require a specific structure like the following: <pre> <code class="language-js"> ... </code> </pre> Quoting Prism.js documentation: Prism encourages good authoring practices and only works with <pre> elements as marking up code without a <pre> element is semantically invalid. While this is commendable, I had my code inside a <div> from an external source....