Timers and countdowns in SwiftUI
Reading time: 1 min
This recipe shows how to implement a timer in SwiftUI in order to update the UI state at a specific interval.
There are two ways of going about it:
- The simple one, with
onReceive
. - A bit more complex, but also more powerful one, using
SimpleTimer
wrapper.
Direct updates with onReceiv...
Reading time: 3 min
Here's a quick tip for resolving a common and annoying error that you'll occasionally see. The error message is
Function declares an opaque return type, but the return statements in its body do not have matching underlying types
and you can see it if you declare a function or a property that...
Star rating view in SwiftUI
Reading time: 1 min
This recipe shows how to implement a star rating view in SwiftUI. The end result will look like this:
The view uses SF Symbols, allowing it to scale to any size and render in any color. It supports an arbitrary upper limit for the rating and automatically rounds to nearest half.
Here's...
SwiftUI Alert with TextField
Reading time: 2 min
This recipe shows how to add a TextField
to a SwiftUI alert
dialog. The end result looks like this:
The dialog supports:
- Custom title and message.
- Text field with a placeholder and keyboard type.
- Confirm button, whose callback returns the content of the text field.
- Optiona...
Reading time: 1 min
Here's a quick recipe for getting the key window, top or root View Controller, in a scene-based SwiftUI app:
extension UIApplication {
var currentKeyWindow: UIWindow? {
UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.map { $0 as? UIWind...