
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Alert
Alert is a special type of pop-up message or notification in SwiftUI displayed to the user. It generally displays some important message to the user before performing a critical action. Alerts are designed to gain the attention or confirmation of the user whenever the user performs any critical task.
An alert box consists of the important message and one or two buttons such as OK, Delete, Cancel, etc. An alert box is commonly used to display information, confirmation, warnings, or error messages. In this chapter, we are going to learn how to create an alert box in SwiftUI.
Creating Alert in SwiftUI
In SwiftUI, we can create an alert box using a pre-defined alert() modifier. This modifier displays the alert dialogue box using the given data. This modifiers are supported by iOS, tvOS, and watchOS only.
To display the alert box isPresented is set to true and the data must have something in it. The value of the data parameter does not change after the display of the alert box.
Syntax
Following is the syntax −
alert( _ title: Text, isPresented: Binding, presenting data: T?, @ViewBuilder actions: (T) -> A ) -> some View where A : View
Parameters
Following are the parameters accepted by the modifier −
title: Represent the title of the alert.
isPresented: A boolean value checks whether the alert is present. The system sets its value to false whenever the user takes action on the alert message.
data: It is an optional source of truth for the given alert. Here system passes the content to the modifier closure. We can display this data in the alert box.
action: It is a viewBuilder which returns the alert's action.
Example 1
The following SwiftUI program creates an alert box.
import SwiftUI struct ContentView: View { @State private var myAlert : Bool = false var body: some View { VStack{ Button("Click Here"){ myAlert = true } .clipShape(Rectangle()) .background(.gray) .foregroundStyle(.white) .font(.largeTitle) .alert("Alert", isPresented: $myAlert){ Button("Ok", role: .cancel){} }message: { Text("If you click on this button your system will hanged") } } } } #Preview { ContentView() }
Output

Example 2
The following SwiftUI program creates an alert box with multiple buttons.
import SwiftUI struct ContentView: View { @State private var myAlert : Bool = false @State private var username = "" @State private var password = "" var body: some View { VStack{ TextField("Username", text: $username).textFieldStyle(.roundedBorder) TextField("passcode", text: $password).textFieldStyle(.roundedBorder) Button("Click Here"){ myAlert = true }.font(.largeTitle) .alert("Alert", isPresented: $myAlert){ Button("Signup", role: .none){} Button("Cancel", role: .cancel){} }message: { Text("Do you want to SignUp or not?") } } } } #Preview { ContentView() }
Output
