Reading time: 2 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...

Continue Reading...

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...

Continue Reading...

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:

  1. Custom title and message.
  2. Text field with a placeholder and keyboard type.
  3. Confirm button, whose callback returns the content of the text field.
  4. Optiona...

Continue Reading...

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...

Continue Reading...

Reading time: 1 min

This tutorial shows how to add Android-like top tabs in SwiftUI. The result will look something like this:

Without much ado, here's the code for the component:

struct Tabs: View {
  @Binding var tabs: [String] // The tab titles
  @Binding var selection: Int // Currently selected...

Continue Reading...