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

SwiftUI iterating through dictionary with ForEach

Is there a way to iterate through a Dictionary in a ForEach loop? Xcode says Generic struct 'ForEach' requires that '[String : Int]' conform to 'RandomAccessCollection' so is there a way to make Swift Dictionaries conform to…
RPatel99
  • 7,448
  • 2
  • 37
  • 45
93
votes
6 answers

What is the difference between ObservedObject and StateObject in SwiftUI

If I have an ObservableObject in SwiftUI I can refer to it as an @ObservedObject: class ViewModel: ObservableObject { @Published var someText = "Hello World!" } struct ContentView: View { @ObservedObject var viewModel = ViewModel() …
superpuccio
  • 11,674
  • 8
  • 65
  • 93
93
votes
8 answers

Alternative to switch statement in SwiftUI ViewBuilder block?

⚠️ 23 June 2020 Edit: From Xcode 12, both switch and if let statements will be supported in the ViewBuilder! I’ve been trying to replicate an app of mine using SwiftUI. It has a RootViewController which, depending on an enum value, shows a different…
Nikolay Marinov
  • 2,381
  • 2
  • 7
  • 12
93
votes
9 answers

Is there a method to blur a background in SwiftUI?

I'm looking to blur a view's background but don't want to have to break out into UIKit to accomplish it (eg. a UIVisualEffectView) I'm digging through docs and got nowhere, seemingly there is no way to live-clip a background and apply effects to it.…
Sonicjet
  • 940
  • 1
  • 6
  • 8
92
votes
8 answers

SwiftUI NavigationLink loads destination view immediately, without clicking

With following code: struct HomeView: View { var body: some View { NavigationView { List(dataTypes) { dataType in NavigationLink(destination: AnotherView()) { HomeViewRow(dataType:…
Nat
  • 12,032
  • 9
  • 56
  • 103
92
votes
14 answers

SwiftUI - Half modal?

I'm trying to recreate a Modal just like Safari in iOS13 in SwiftUI: Here's what it looks like: Does anyone know if this is possible in SwiftUI? I want to show a small half modal, with the option to drag to fullscreen, just like the sharing…
ryannn
  • 1,329
  • 1
  • 9
  • 21
91
votes
9 answers

How to disable user interaction on SwiftUI view?

Let's say I have a SwiftUI view hierarchy that looks like this: ZStack() { ScrollView { ... } Text("Hello.") } The Text view blocks touch events from reaching the underlying ScrollView. With UIKit, I'd use something like…
chockenberry
  • 7,811
  • 5
  • 32
  • 41
91
votes
17 answers

SwiftUI dismiss modal

Since SwiftUI is declarative there is no dismiss method. How can is add a dismiss/close button to the DetailView? struct DetailView: View { var body: some View { Text("Detail") } } struct ContentView : View { var body: some View { …
Ugo Arangino
  • 2,802
  • 1
  • 18
  • 19
90
votes
9 answers

How to scale text to fit parent view with SwiftUI?

I'd like to create a text view inside a circle view. The font size should be automatically set to fit the size of the circle. How can this be done in SwiftUI? I tried scaledToFill and scaledToFit modifiers, but they have no effect on the Text…
G. Marc
  • 4,987
  • 4
  • 32
  • 49
88
votes
6 answers

An equivalent to computed properties using @Published in Swift Combine?

In imperative Swift, it is common to use computed properties to provide convenient access to data without duplicating state. Let's say I have this class made for imperative MVC use: class ImperativeUserManager { private(set) var currentUser:…
rberggreen
  • 983
  • 1
  • 6
  • 6
87
votes
10 answers

Transition animation not working properly in SwiftUI

I'm trying to create a really simple transition animation that shows/hides a message in the center of the screen by tapping on a button: struct ContentView: View { @State private var showMessage = false var body: some View { …
superpuccio
  • 11,674
  • 8
  • 65
  • 93
86
votes
12 answers

How to make a SwiftUI List scroll automatically?

When adding content to my ListView, I want it to automatically scroll down. I'm using a SwiftUI List, and a BindableObject as Controller. New data is getting appended to the list. List(chatController.messages, id: \.self) { message in …
Seb
  • 1,586
  • 1
  • 11
  • 15
84
votes
11 answers

How to display Image from a url in SwiftUI

So I'm trying to create a content feed using data fetched from my Node JS server. Here I fetch data from my API class Webservice { func getAllPosts(completion: @escaping ([Post]) -> ()) { guard let url = URL(string:…
DDavis25
  • 1,149
  • 1
  • 13
  • 25
84
votes
8 answers

SwiftUI - Unable to change color of Image icon

I am trying to build my own custom tab bar view, while building my custom buttons I am unable to change the color of Image(). struct TabBarButton: View { let title: String let icon: String var body: some View { …
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50
84
votes
22 answers

SwiftUI: Status bar color

Is there a way to change the status bar to white for a SwiftUI view? I'm probably missing something simple, but I can't seem to find a way to change the status bar to white in SwiftUI. So far I just see .statusBar(hidden: Bool).
keegan3d
  • 10,357
  • 9
  • 53
  • 77