Questions tagged [swiftui]

SwiftUI is a way to build user interfaces across all Apple platforms. Build user interfaces for any Apple device using one set of tools and APIs. With a declarative Swift syntax that’s easy to read and natural to write, SwiftUI works seamlessly with Xcode design tools to keep your code and design perfectly in sync. Automatic support for Dynamic Type, Dark Mode, localization, and accessibility. Use this tag for questions about SwiftUI on any platform.

SwiftUI is a closed-source Apple framework providing a declarative Swift-only API for defining the appearance and behavior of graphical user interfaces. Apple's reference documentation describes SwiftUI as follows:

SwiftUI provides views, controls, and layout structures for declaring your app's user interface. The framework provides event handlers for delivering taps, gestures, and other types of input to your app, and tools to manage the flow of data from your app's models down to the views and controls that users will see and interact with.

Though apps can now be built fully with SwiftUI, using the SwiftUI lifecycle, one can also embed the top-level SwiftUI View in an appropriate hosting adapter for the platform (NSHostView, NSHostingController, UIHostingController, or WKHostingController) for apps with non-SwiftUI lifecycles.

SwiftUI integrates with Apple's older imperative AppKit, UIKit, and WatchKit frameworks. An NSView or UIView can embed a SwiftUI View and vice versa.

SwiftUI supports live previews and dynamic replacement (hot swapping) in Xcode 11 on macOS 10.15 (Catalina) and later, and on devices running an operating system that supports SwiftUI (see the list below).

Apple first revealed SwiftUI at WWDC on June 3, 2019 and made it available in the first beta release of Xcode 11.

SwiftUI is part of the following SDKs:

  • macOS 10.15 (Catalina) and later,
  • iOS 13 and later,
  • tvOS 13 and later,
  • watchOS 6 and later.

SwiftUI apps cannot be deployed to older platforms.

In addition to the official documentation, Apple offers two tutorials (Introducing SwiftUI and the first part of Develop Apps for iOS) entirely devoted to SwiftUI, as well as sample code (Fruta: Building a Feature-Rich App with SwiftUI).

35593 questions
10
votes
2 answers

SwiftUI Using MapKit for Address Auto Complete

I have a form where the user enters their address. While they can always enter it manually, I also wanted to provide them with an easy solution with auto complete so that they could just start typing their address and then tap on the correct one…
kittonian
  • 1,020
  • 10
  • 22
10
votes
4 answers

How can I hide title bar in SwiftUI? (for MacOS app)

I know how to hide the title bar with Storyboard. But I can't do this in SwiftUI. I want to hide the title bar and the control buttons and make a floating image view.
Retriever
  • 170
  • 1
  • 11
10
votes
1 answer

SwiftUI: NavigationLink pops out immediately on WatchOS 8.1RC in Tabview

I have discovered a regression in watchOS 8.1RC with NavigationLink triggered from a TabView. It's immediately dismissed. It was working in watchOS 8.0 or in Simulator (watchOS 8.0). Do you know a workaround ? Thanks Sample code: import…
10
votes
1 answer

SwiftUI LazyVGrid cell height in iOS 14 vs 15

I have some code that was working in iOS 14, but in 15, isn’t quite. The cells pictured below can have one or two lines of title. In iOS 14 (on the right), the Spacer() element between the title and the listing count text expands as expected so that…
Rick
  • 3,298
  • 3
  • 29
  • 47
10
votes
3 answers

SwiftUI Custom Picker Label Not Rendering

After updating to iOS 15 and Xcode 13, my picker in my app is no longer showing a custom label. Running the app on an iOS 14 device, the pickers render fine. This is the code snippet that is currently implemented and the screenshot is what it…
Talon
  • 811
  • 7
  • 22
10
votes
1 answer

Convert SwiftUI View to NSImage

How can I create a SwiftUI View extension to return a NSImage? I have seen how it can be done on iOS using UIGraphicsImageRenderer but it seems there is no macOS equivalent.
Rom4in
  • 532
  • 4
  • 13
10
votes
5 answers

iOS 15: How to display ATT dialog when the app starts in SwiftUI

In iOS 14, It could display ATT (App Tracking Transparency) dialog when app starts in SwiftUI as follows. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { …
donchan
  • 197
  • 1
  • 8
10
votes
1 answer

Generic parameter 'T' could not be inferred - Swift 5.5

I'm trying to get the user sign in and I'm getting the following error: Generic parameter 'T' could not be inferred This is the code: // Gets User signed-in func getUser() async throws -> AuthUser { do { try await…
Arturo
  • 3,254
  • 2
  • 22
  • 61
10
votes
1 answer

Communication between ViewModels with SwiftUI and Combine (ObservableObject vs Binding)

This is a general question about SwiftUI and the architecture, so I'll take a simple but problematic example. Initial project : I have a first View which displays a list of Items. This list is managed by a class (which I called ListViewModel here).…
Adrien
  • 1,579
  • 6
  • 25
10
votes
2 answers

SwiftUI AppDelegate not being ran whenever app is opened

Here is a simple AppDelegate that I have made: import SwiftUI class AppDelegate: UIResponder, UIApplicationDelegate { private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { …
Kunal Katiyar
  • 304
  • 2
  • 15
10
votes
2 answers

ToolbarItem with a Menu very hard to tap - SwiftUI

I try to create a (add/plus) button that shows a menu from which we can select the action we want to execute (it is similar to the Files, or Notes app on iPhone) .navigationTitle("Hello World Everyone") .toolbar { ToolbarItem(placement:…
LetsGoBrandon
  • 498
  • 8
  • 23
10
votes
2 answers

SwiftUI - detecting Long Press while keeping TabView swipable

I'm trying to detect a Long Press gesture on TabView that's swipable. The issue is that it disables TabView's swipable behavior at the moment. Applying the gesture on individual VStacks didn't work either - the long press doesn't get detected if I…
Kirill
  • 738
  • 10
  • 26
10
votes
5 answers

Fix line spacing in custom font in SwiftUI

I am using custom font (Catamaran) and it seems like it has big space between lines in it. For example I have this code: Text("Example text that has big space between lines") .lineSpacing(0) .padding(.horizontal) …
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
10
votes
0 answers

SwiftUI - Using Different Transitions on Nested Views

How can I have an overlaying view in SwiftUI with a different transition on different portions? E.g. in my concrete example, I want to achieve the following: one subview of the overlay view slides in from the bottom another subview of the overlay…
Leo
  • 1,508
  • 13
  • 27
10
votes
3 answers

Passing environment object between non-view classes in SwiftUI

I understand that EnvironmentObject property wrapper can be used to pass around objects to views. I have a session object which I am passing around to my views. Now I have a requirement to pass this into one of my model classes (i.e., non-view).…
Pradyot
  • 2,897
  • 7
  • 41
  • 58