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

SwiftUI HStack with Wrap

Is it possible that the blue tags (which are currently truncated) are displayed completely and then it automatically makes a line break? NavigationLink(destination: GameListView()) { VStack(alignment: .leading, spacing: 5){ // Name der…
Flolle
  • 733
  • 1
  • 7
  • 9
69
votes
27 answers

How to create TextField that only accepts numbers

I'm new to SwiftUI and iOS, and I'm trying to create an input field that will only accept numbers. TextField("Total number of people", text: $numOfPeople) The TextField currently allows alphabetic characters, how do I make it so that the user can…
Lupyana Mbembati
  • 1,516
  • 2
  • 14
  • 19
68
votes
5 answers

Picker for optional data type in SwiftUI?

Normally I can display a list of items like this in SwiftUI: enum Fruit { case apple case orange case banana } struct FruitView: View { @State private var fruit = Fruit.apple var body: some View { Picker(selection:…
Senseful
  • 86,719
  • 67
  • 308
  • 465
68
votes
4 answers

How do you use .enumerated() with ForEach in SwiftUI?

Here's a simple SwiftUI List that works as expected: struct App: View { let items = Array(100...200) var body: some View { List { ForEach(items, id: \.self) { index, item in Text("Item \(item)") } }.frame(width: 200,…
Gil Birman
  • 35,242
  • 14
  • 75
  • 119
67
votes
6 answers

How to update @FetchRequest, when a related Entity changes in SwiftUI?

In a SwiftUI View i have a List based on @FetchRequest showing data of a Primary entity and the via relationship connected Secondary entity. The View and its List is updated correctly, when I add a new Primary entity with a new related secondary…
Björn B.
  • 753
  • 1
  • 7
  • 10
67
votes
5 answers

How can I create a button with image in SwiftUI?

I created a button in SwiftUI with these line of codes: Button(action: { print("button pressed") }) { Image("marker") } but marker image automatically changes to blue color. I want to use original image in button. this is original…
Sajjad
  • 1,536
  • 2
  • 17
  • 31
66
votes
2 answers

What does the dollar sign do in Swift / SwiftUI?

This tutorial by Apple about SwiftUI uses a dollar sign to bind data, and I‘m having trouble finding more information about this data binding in SwiftUI. Toggle(isOn: $showFavoritesOnly) { You use the $ prefix to access a binding to a state…
vrwim
  • 13,020
  • 13
  • 63
  • 118
66
votes
6 answers

Is there any way to use storyboard and SwiftUI in same iOS Xcode project?

As Swift 5 introduces the SwiftUI framework for creating the views, but we are currently using the storyboard for UI design. So I just wanted to know the procedure to use Storyboard and Swift UI in same iOS Single View Application.
Nirbhay Singh
  • 1,204
  • 1
  • 12
  • 16
66
votes
8 answers

How to set Keyboard type of TextField in SwiftUI?

I can't seem to find any information or figure out how to set the keyboard type on a TextField for SwiftUI. It would also be nice to be able to enable the secure text property, to hide passwords etc. This post shows how to "wrap" a UITextField, but…
Aaron Bertsch
  • 700
  • 1
  • 5
  • 8
65
votes
6 answers

How to assign an optional Binding parameter in SwiftUI?

I'm trying to build a custom NavBar with some optional Views, like a searchbar (but only if the view needs to display it). I need to pass some @State properties with @Binding down the views, basically. But I also need them to be Optional…
riciloma
  • 1,456
  • 2
  • 16
  • 31
64
votes
4 answers

SwiftUI: How to remove margin between views in VStack?

Using SwiftUI, I created a VStack, which contains some fixed elements and a list element. The reason is, that the user should only scroll the area under the fixed elements. Now I see a space between the second fixed element and the list. I don't…
tosi
  • 787
  • 1
  • 7
  • 12
64
votes
6 answers

SwiftUI Optional TextField

Can SwiftUI Text Fields work with optional Bindings? Currently this code: struct SOTestView : View { @State var test: String? = "Test" var body: some View { TextField($test) } } produces the following error: Cannot convert…
Brandon Bradley
  • 3,160
  • 4
  • 22
  • 39
64
votes
3 answers

SwiftUI: Forcing an Update

Normally, we're restricted from discussing Apple prerelease stuff, but I've already seen plenty of SwiftUI discussions, so I suspect that it's OK; just this once. I am in the process of driving into the weeds on one of the tutorials (I do that). I…
Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
64
votes
2 answers

Multiline Text View in SwiftUI

How to set Text View to display text in multiple lines? By default it's 1 line. struct ContentView : View { var body: some View { Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse est leo, vehicula eu eleifend…
Bilal
  • 18,478
  • 8
  • 57
  • 72
64
votes
16 answers

UICollectionView and SwiftUI?

How to create grid of square items (for example like in iOS Photo Library) with SwiftUI? I tried this approach but it doesn't work: var body: some View { List(cellModels) { _ in Color.orange.frame(width: 100, height: 100) } } List…
Evgeny Mikhaylov
  • 1,696
  • 1
  • 19
  • 25