Posts

AsyncImage - SwiftUI

Image
  AsyncImage is a built-in SwiftUI view introduced in iOS 15+ to asynchronously load and display images from a  URL .  It provides a simple way to load images without writing URLSession code to download data in background and set it to ImageView on Main thread . import SwiftUI struct AsyncImageView : View {     var images : [ URL ] = [         URL ( string : "https://picsum.photos/id/1000/400/400" )!,         URL ( string : "https://picsum.photos/id/1001/400/400" )!,         URL ( string : "https://picsum.photos/id/1002/400/400" )!,         URL ( string : "https://picsum.photos/id/1003/400/400" )!,         URL ( string : "https://picsum.photos/id/1004/400/400" )!,         URL ( string : "https://picsum.photos/id/1005/400/400" )!,         URL ( string : "https://picsum.photos/id/1006/400/400" )!,       ...

PickerView In SwiftUI

Image
As we know to PickerView is similar to a Dropdown in iOS. You can select from a set of provided  items within a PickerView. So let's get started and add a simple PickerView where user can select a Number from 1 to 10. Code Snippet: struct ContentView : View {     var body : some View {         Picker (selection: . constant ( 4 ), label: Text ( "Select Numbers" )) {             ForEach ( 1 ..< 11 ) { index in                 Text ( " \(index) " ). tag (index)             }         }     } } Explanation: Picker takes two parameters here, selection and label . selection means the item which you want to be selected by default (number 5 here). label means the Title of Picker (Select Numbers here). Inside PickerView I have passed TextView's with each having number indexing 1 to 10. Hence th...

Alert & Action Sheet in SwiftUI

Image
Alerts & Action Sheets do work in similar fashion as they do previously in Swift or Objective C. Below are the codes to achieve Alert and Action Sheet in SwiftUI. Both of these do have Title, Message and Buttons (you can add actions to them). Alert Sheet Code Snippet: struct ContentView : View {     @ State var choosenAction = "Hit the button to show Alert"     @ State var showAlert : Bool = false     var body : some View {         VStack (spacing: 50 ) {             Text ( choosenAction )                 . font (. largeTitle )                 . foregroundColor (. green )             Button (action: {                 self . showAlert . toggle ()             }) {       ...

Grid in SwiftUI

Image
In previous posts we have worked on TableView (List), now you would be curious to know about the CollectionView or Grid in SwiftUI. So there is no such thing as CollectionView in SwiftUI. Oh.. noooo... Lets discuss how to get a Grid in SwiftUI. Code Snippet: struct ContentView : View {     var body : some View {         List {             ForEach ( 0 ..< 10 ) { row in                 HStack (spacing: 20 ) {                     ForEach ( 0 ..< 3 ) { column in                         ZStack {                             Color (. red )                                 . cornerRadius ( 20 ) ...

Button in SwiftUI

Image
Lets learn how to work with a Button in SwiftUI. Let me explain the code snippet: 1. Two private variables, one is buttonClicked which is a Bool that will toggle on button click action and the other is textOnLabel is a String with a default as BLACK. 2. In body I have a VStack with spacing 50, it will add a spacing of 50 between UIControls added inside the stack. 3. Inside this VStack; I wanted to have a Text (to display the Background Color of Button) and a Button . 4. Inside Button's action closure I toggled the bool value of var  buttonClicked and made a call to method buttonAction() . 5. Inside this  buttonAction  method I just changed the text of  textOnLabel  based on the  buttonClicked bool value. 6. To make Button UI better I added padding, background color, foreground color and cornerradius to it. struct ContentView : View {     @ State private var buttonClicked = false     @ State private var tex...