IOS TOPICS

IOS Introduction
Swift UI Label
Swift UI Button
Swift UI Text Field
Swift UI Slider
Swift UI Switch
Swift UI Stepper
Swift UI Date Picker
Swift UI Segmented Control
Views
Table View
Collection Views
SCROLL VIEW
iOS Content View
iOS View Controllers
iOS Bar interface
iOS Navigation interface
iOS Architect Pattern
iOS Libraries
iOS Web request and parsing
iOS user defaults
iOS Coredata and database

SwiftUI:Buttons


What are Buttons in SwiftUI?

Buttons as widely understood, interact with users to launch a single or a series of predefined actions. In SwiftUI, buttons are flexible and quite customizable (more so than UIButtons in the UIKit). Let’s learn how to use and customize buttons on Xcode to fit our projects.

Declaration:

Buttons are structures, so we must instantiate the button as below

Button(action:{ //Code to define post-click action }) { //look and feel of the button }

The first half defines the kind of action the button will perform ex. Toggle between two screens, etc. The second aspect defines how the button looks to users. Note: For text only buttons, you may use a simpler syntax that takes the first parameter as a localized string key.

Creating the Simplest Form of a Button: Click on a Text button to display a message

Code:

import SwiftUI struct ContentView: View { var body: some View { Button(action:{ print("You are here") }) { Text("Location") } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

Output:

On running this code, we can click the pin icon on the simulator and the message is displayed on the console:

ios output