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
64
votes
7 answers

What does the SwiftUI `@State` keyword do?

The SwiftUI tutorial uses the @State keyword to indicate mutable UI state: @State var showFavoritesOnly = false It offers this summary: State is a value, or a set of values, that can change over time, and that affects a view’s behavior, content, or…
Taylor
  • 5,871
  • 2
  • 30
  • 64
63
votes
7 answers

SwiftUI: Putting a LazyVStack or LazyHStack in a ScrollView causes stuttering (Apple bug??)

XCode Version 12.4 (12D4e) I have encountered this every time I have implemented a Lazy stack within a ScrollView: Add a LazyHStack to a horizontal ScrollView or a LazyVStack to a vertical ScrollView Add enough content such that the content size of…
chrysb
  • 1,351
  • 2
  • 13
  • 20
63
votes
11 answers

How to use Attributed String in SwiftUI

How to use AttributedString in SwiftUI. There is no API available to use AttributedString in Text
Dattatray Deokar
  • 1,923
  • 2
  • 21
  • 31
63
votes
5 answers

How can I hide the navigation back button in SwiftUI?

navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View But it still shows the back button and I want to remove the back function when clicked.
Daniel Kua
  • 763
  • 1
  • 5
  • 3
62
votes
4 answers

How do you check if SwiftUI is in preview mode?

Is there a way to check if a SwiftUI app is in preview mode? For example, you can check if your app is in development or production mode using #if DEBUG. Can you do something similar to check if you're previewing or not?
Ken Mueller
  • 3,659
  • 3
  • 21
  • 33
62
votes
12 answers

How to remove highlight on tap of List with SwiftUI?

How to remove highlight on tap of List with SwiftUI? List { }.whatModifierToAddHere? The selection manager documentation doesnt say anything about it.
Just a coder
  • 15,480
  • 16
  • 85
  • 138
61
votes
1 answer

SwiftUI -> Thread 1: Fatal error: No observable object of type MyObject.Type found (EnvironmentObject in sheet)

I'm building an app with SwiftUI. When I was trying to display a sheet (previously Modal), this error message appears: Thread 1: Fatal error: No observable object of type BixiStationCombinedListViewModel.Type found. A View.environmentObject(_:) for…
Daymo502
  • 825
  • 1
  • 6
  • 10
61
votes
9 answers

SwiftUI Picker onChange or equivalent?

I want to change another unrelated @State variable when a Picker gets changed, but there is no onChanged and it's not possible to put a didSet on the pickers @State. Is there another way to solve this?
trapper
  • 11,716
  • 7
  • 38
  • 82
61
votes
5 answers

@Binding and ForEach in SwiftUI

I can't undertand how to use @Binding in combination with ForEach in SwiftUI. Let's say I want to create a list of Toggles from an array of booleans. struct ContentView: View { @State private var boolArr = [false, false, true, true, false] …
superpuccio
  • 11,674
  • 8
  • 65
  • 93
61
votes
4 answers

How to adjust spacing between HStack elements in SwiftUI?

I have added spacer(minLength: 5) but it takes the minLength. How can I specify the spacing between the text? I have attached a screenshot for reference. I want to reduce the spacing between inner HStack. HStack { …
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
60
votes
5 answers

Cannot convert value of type 'Published.Publisher' to expected argument type 'Binding'

When trying to compile the following code: class LoginViewModel: ObservableObject, Identifiable { @Published var mailAdress: String = "" @Published var password: String = "" @Published var showRegister = false @Published var…
Jan Koch
  • 603
  • 1
  • 5
  • 4
60
votes
6 answers

How to set addObserver in SwiftUI?

How do I add NotificationCenter.default.addObserve in SwiftUI? When I tried adding observer I get below error Argument of '#selector' refers to instance method 'VPNDidChangeStatus' that is not exposed to Objective-C But when I add @objc in front…
O-mkar
  • 5,430
  • 8
  • 37
  • 61
60
votes
3 answers

What is Geometry Reader in SwiftUI?

I am learning SwiftUI. And I come across to "GeometryReader". And I want to know why and when to use it?
Md Shafiul Islam
  • 1,579
  • 1
  • 13
  • 19
60
votes
10 answers

Prevent dismissal of modal view controller in SwiftUI

At WWDC 2019, Apple announced a new "card-style" look for modal presentations, which brought along with it built-in gestures for dismissing modal view controllers by swiping down on the card. They also introduced the new isModalInPresentation…
Jumhyn
  • 6,687
  • 9
  • 48
  • 76
59
votes
20 answers

How to add placeholder text to TextEditor in SwiftUI?

When using SwiftUI's new TextEditor, you can modify its content directly using a @State. However, I haven't see a way to add a placeholder text to it. Is it doable right now? I added an example that Apple used in their own translator app. Which…
Legolas Wang
  • 1,951
  • 1
  • 13
  • 26