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
11
votes
5 answers

SwiftUI only showing a black screen

I had created a few projects in SwiftUI that were perfectly fine, before I went on holiday around 2 weeks ago. When I got back, I updated Xcode and my iPhone. The code for SwiftUI is not relevant as it was all working perfectly fine before. All I am…
George
  • 25,988
  • 10
  • 79
  • 133
11
votes
1 answer

SwiftUI @Binding doesn't refresh View

I have a simple master/detail interface where the detail view modifies an item in an array. Using the below, the model is updated properly, but SwiftUI doesn't refresh the View to reflect the change. Model: struct ProduceItem: Identifiable { let…
smr
  • 890
  • 7
  • 25
11
votes
1 answer

Is it possible to set SwiftUI DatePicker minimumDate using bindings?

I am trying to set the minimum date value of a date picker in SwiftUI using @Binding and @State. I have three steppers, one for days, one for hours, and one for minutes. These are used to set the values of DateComponents. What I want to see is the…
K. Law
  • 2,541
  • 3
  • 18
  • 27
11
votes
1 answer

How do I bind a SwiftUI element to a value in a Dictionary?

I have a dictionary that contains various values I want to "filter" by. So I'm doing something like this struct ExampleView : View { @EnvironmentObject var externalData : ExternalData var body: some View { VStack { …
Phylliida
  • 4,217
  • 3
  • 22
  • 34
11
votes
6 answers

InputAccessoryView / View Pinned to Keyboard with SwiftUI

Is there an equivalent to InputAccessoryView in SwiftUI (or any indication one is coming?) And if not, how would you emulate the behavior of an InputAccessoryView (i.e. a view pinned to the top of the keyboard)? Desired behavior is something like…
zzz
  • 613
  • 1
  • 8
  • 13
11
votes
3 answers

Device-specific Layout with SwiftUI on Apple Watch and iPhone

Sometimes, I need to make device-specific adjustments to layouts. For example, I may need to reduce spacing on an iPhone with a smaller screen or increase spacing on the largest screens. With UIKit (and even Interface Builder) it was easy to make…
gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
11
votes
2 answers

SwiftUI Beta 3 Black Screen

I just converted to beta 3 and my previously working SwiftUI code is now rendering a plain black screen. Was there a change in beta 3 that is causing this. Is there a solution to fix it? Scene delegate code: func scene(_ scene: UIScene,…
David L
  • 4,347
  • 3
  • 18
  • 30
11
votes
2 answers

Execute code when SecureField gets focused/unfocused in SwiftUI

I have a SecureField in SwiftUI. When this Field gets focused (when the user taps it) I want to manipulate a @State property for applying a offset modifier for the SecureField. When the SecureField gets out of focus the @State property should get…
Womble Tristes
  • 393
  • 1
  • 4
  • 14
11
votes
3 answers

Xcode SwiftUI how to scroll the Preview Canvas?

I have started working with SwiftUI Demo. But I am not able to scroll the Preview canvas in Xcode. Yes there is button for zooming like - 100 + . I have seen a video where person is scrolling this canvas during the tutorial is there any option…
Plutomen DEV
  • 177
  • 2
  • 9
11
votes
1 answer

SwiftUI memory leak when using UIViewControllerRepresentable

UPDATE: As of beta4, problem still present. I have created a very simple example of how a UIViewController represented by UIViewControllerRepresentable is never deallocated. import SwiftUI struct ContentView : View { @State private var…
kontiki
  • 37,663
  • 13
  • 111
  • 125
11
votes
5 answers

SwiftUI ScrollView only scroll in one direction

Trying to make a custom list using a view as the list row style (to get rid of the ugly line separates in the list by default). However, once I put my ZStack rows inside a scroll view, the scroll view scrolls in both directions and not just…
mg_berk
  • 731
  • 2
  • 8
  • 12
11
votes
2 answers

SwiftUI frame size

How do you get the frame size in UIViewRepresentable? I have a simple DrawView class that draw something and I would like to integrate with SwiftUI. This code works if I hardcode the frame size to 640x480, but is it possible to know the current…
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88
11
votes
3 answers

IndexSet referring to index of the section instead of the row

I want to implement a swipe-to-delete functionality on a SwiftUI Section by using the .onDelete modifier. The problem is that it always deletes the first item in the list. My view has a list with dynamic sections created with a ForEach. struct…
KrohnicDev
  • 355
  • 2
  • 12
11
votes
6 answers

Measure the rendered size of a SwiftUI view?

Is there a way to measure the computed size of a view after SwiftUI runs its view rendering phase? For example, given the following view: struct Foo : View { var body: some View { Text("Hello World!") .font(.title) …
Sean Rucker
  • 1,066
  • 2
  • 11
  • 20
11
votes
2 answers

Get a UIViewControllerRepresentableContext environment value?

Ok, so this might be trivial, but I am not sure how to go about this. I have a UIViewController that gets created when the SwiftUI view calls: func makeUIViewController(context: Context) -> MyViewController The View that makes that call was given…
zumzum
  • 17,984
  • 26
  • 111
  • 172