Swift Standard Library provides a randomElement() method to return a random element from an array or nil if the array is empty. If you know that your array is not empty, then go ahead and force unwrap the result of randomElement() method: let array = [“Swift”, “SwiftUI”, “UIKit”, “Foundation”] let randomElement = array.randomElement()! print(randomElement) However, …
How to add borders to SwiftUI views
Learn how to add borders to SwiftUI views including: Text view, Stack view, Image view, and Button. Create rectangular or rounded-corners borders using .border() or .overlay() modifiers respectively. Create solid line or dashed borders. Add default, rectangular border to a Text view using .border() modifier: Text(“SwiftUI Tutorials”) .font(.largeTitle) .padding() .border(Color.black) Add rectangular border to a …
Advanced SwiftUI button styling and animation
Learn how to style SwiftUI buttons using ButtonStyle protocol and .buttonStyle() modifier by creating reusable button styles. Using ButtonStyle protocol allows you to create advanced button styles and introduce animations to the button style when it is being pressed. Let’s start with a simple button: Button(action: { print(“Button action”) }) { HStack { Image(systemName: “bookmark.fill”) Text(“Bookmark”) …
How to create a Button in SwiftUI
Learn how to create and use Buttons in SwiftUI. Style the button with text and images, add border around it. Perform actions when button is pressed and update SwiftUI view accordingly. SwiftUI Button is similar to UIButton from UIKit when it comes to behavior however you define it in a different way by specifying an …
How to use SF Symbols in SwiftUI
While this tutorial mostly focuses on using SF Symbols in SwiftUI, there is a section at the end which covers how to use SF Symbols in UIKit. What are SF Symbols? I think Apple’s documentation explains it best: SF Symbols provides a set of over 1,500 consistent, highly configurable symbols you can use in your …
How to convert String to Double or Float in Swift
Converting a String to a Double or Float can be easily achieved by using the if let statement in Swift. Take a look at the following example: var string = “12.2416” if let floatValue = Float(string) { print(“Float value = \(floatValue)”) } else { print(“String does not contain Float”) } If the String contains a valid …
Floating point precision in Swift String
If you’d like to interpolate a float or a double with a String in Swift, use the %f String format specifier with appropriate number in order to specify floating point precision. Given a Float or a Double value: var value = 4.53978 Create a string with value rounded to one decimal place which results in …
How to add text overlay on image in SwiftUI
Learn how to add text overlay on top of an image in SwiftUI. Achieve it by using the .overlay() modifier which layers a second view in front of other view. First of all, import an image into your Xcode project. I won’t go into details how to achieve it but if you’d like to learn, …
SwiftUI Image tutorial
Learn how to use Images in SwiftUI, resize images, adjust their content ratio, style them and more. This tutorial will get you started with SwiftUI Images. In the following examples we are going to use a photo downloaded from Unsplash. In order to add it to your Xcode project, download it to your computer, click …
How to build a linear progress bar in SwiftUI
In this tutorial we’re going to learn how to build a horizontal, linear progress bar indicator in SwiftUI. The sample app will include two buttons – one will simulate the progress and will keep updating the progress bar with new value, and the other will simply reset the progress bar to its initial state. Start …