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
2 answers

SwiftUI - Accessing iOS 13's semantic colors

iOS 13 introduces semantic colors: a way of specifying what a color's purpose is rather than its actual value. This allows the color to automatically adapt when dark mode is enabled. In UIKit, these colors can be easily accessed via static members…
Jacob
  • 402
  • 3
  • 14
11
votes
3 answers

Xcode 11 -- SwiftUI's dark mode setup

Okay. I know this shouldn't be rocket science. I can't seem to get dark mode working and I've read the documentation a few times. Hoping someone can pick out what I'm missing. I have an named color in the asset catalog. I set my plist mode to be…
Craig Fisher
  • 371
  • 2
  • 15
11
votes
4 answers

Repeating Action Continuously In SwiftUI

How can I make an element such as a text field scale up and then down continuously? I have this: struct ContentView : View { @State var size:Double = 0.5 var body: some View { ZStack { Text("Hello!") …
Isaac
  • 645
  • 1
  • 7
  • 17
11
votes
4 answers

How to fill shape with Gradient in SwiftUI

how can i pass LinearGradient to a shape (for example Rectangle) just in SwiftUI? Rectangle().frame(width: UIScreen.main.bounds.width, height: 200)
Sajad Beheshti
  • 679
  • 3
  • 8
  • 20
10
votes
3 answers

SwiftUI: strange offset on tap when reopening app with a `sheet` open

I am facing out a strange behavior that really looks like a SwiftUI bug. When I leave the app with a .sheet open and reopen it, all content from parent has an offset on tap. It is difficult to explain (and English is not my mother tongue) so here is…
Jonathan
  • 606
  • 5
  • 19
10
votes
1 answer

Creating line chart with multiple lines

Looking over Apple Documentation I found a straightforward way of doing this provided your dataset is structured correctly (mine is not). I've been working with a CSV file containing rows structured as below: PM2.5 data, PM10 data, DateTime PM2.5…
John Harrington
  • 1,314
  • 12
  • 36
10
votes
0 answers

Why does ForEach initialize its Child View twice as much in SwiftUI?

I came across a weird behaviour of SwiftUI's ForEach view. I've noticed that ForEach always initialize its child view twice as much as it should according to its repetition. Normally, when you render the view, its init is called once. But if you put…
Hollycene
  • 287
  • 1
  • 2
  • 12
10
votes
4 answers

App crashes when SwiftUI view with Picker disappears (since iOS 16)

We have an application with some 'chat' functionality where questions are asked and the user can answer with some predefined options: for every question a new view is presented. One of those options is a view with a Picker, since iOS 16 this Picker…
Vama
  • 208
  • 1
  • 13
10
votes
3 answers

SwiftUI ShareLink run code before sharesheet is shown

I have a ShareLink to share an image ShareLink(item: image) { Image(systemName: "square.and.arrow.up") } Now before I can share this image, I have to generate it with some function @State var image: UIImage var getImage() { // some code…
charelf
  • 3,103
  • 4
  • 29
  • 51
10
votes
2 answers

SwiftUI ShareLink set file name

I'm using the ShareLink to share an FileDocument which contains a String. The FileDocument is conform to the Transferable protocol. This is the FileDocument Struct: struct TransferableDocument: FileDocument, Transferable { static var…
Patrick
  • 273
  • 2
  • 6
10
votes
1 answer

SwiftUI Plain List How to remove top header padding in iOS16

I'm looking for a way to remove top section padding in my SwiftUI List With iOS 15, we can do UITableView.appearance().sectionHeaderTopPadding = 0 However, with iOS 16, List has been reimplemented with UICollectionView and I couldn't find a way to…
Ryan_Chau
  • 233
  • 2
  • 19
10
votes
0 answers

Adding search bar to the list without NavigationView

I have created a custom navigation bar in SwiftUI, now I am trying to add a search bar and make the List searchable without system built NavigationView. I was wondering is there any possible way to do so?
Mc.Lover
  • 4,813
  • 9
  • 46
  • 80
10
votes
1 answer

SwiftUI: Issue with longer text in TextField on macOS

When I enter (or paste) text in a TextField on macOS and the string requires more space than available in TextField I would expect some sort of scrolling behaviour that moves along with the text cursor - like in almost every text field in the world.…
viedev
  • 869
  • 1
  • 6
  • 16
10
votes
1 answer

Difference between @Binding and Binding in Swift

I understand that @Binding is a property wrapper and I believe that Binding is a form of type casting but what is the difference in practical terms? For example declaring a var like: @Binding var hidden: Bool versus var hidden: Binding
pinglock
  • 982
  • 2
  • 12
  • 30
10
votes
1 answer

How to render Markdown headings in SwiftUI AttributedString?

I've been trying to use the new AttributedString that released with iOS 15 to render Markdown stored in a variable. However, I haven't been able to find a way for it to render markdown headings such as: # Title 1 ### Title 3 ###### Title 6 Here's…
Clément Cardonnel
  • 4,232
  • 3
  • 29
  • 36