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
110
votes
15 answers

How to tell SwiftUI views to bind to nested ObservableObjects

I have a SwiftUI view that takes in an EnvironmentObject called appModel. It then reads the value appModel.submodel.count in its body method. I expect this to bind my view to the property count on submodel so that it re-renders when the property…
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
110
votes
2 answers

What enables SwiftUI's DSL?

It seems like Apple's new SwiftUI framework uses a new kind of syntax that effectively builds a tuple, but has another syntax: var body: some View { VStack(alignment: .leading) { Text("Hello, World") // No comma, no separator ?! …
fredpi
  • 8,414
  • 5
  • 41
  • 61
105
votes
5 answers

NavigationLink Works Only for Once

I was working on an application with login and after login there are categories listed. And under each category there are some items listed horizontally. The thing is after login, main page appears and everything is listed great. When you click on…
C.Aglar
  • 1,290
  • 2
  • 14
  • 30
104
votes
12 answers

SwiftUI - Is there a popViewController equivalent in SwiftUI?

I was playing around with SwiftUI and want to be able to come back to the previous view when tapping a button, the same we use popViewController inside a UINavigationController. Is there a provided way to do it so far ? I've also tried to use…
Alexandre Legent
  • 1,049
  • 2
  • 8
  • 6
103
votes
14 answers

SwiftUI NavigationView navigationBarTitle LayoutConstraints issue

This is maybe related to the new (12.3) version of XCode that came out recently but I have a very simple SwiftUI View: import SwiftUI struct HomeView: View { var body: some View { NavigationView { Text("Text") …
goddamnyouryan
  • 6,854
  • 15
  • 56
  • 105
102
votes
5 answers

Why is an ObservedObject array not updated in my SwiftUI application?

I'm playing with SwiftUI, trying to understand how ObservableObject works. I have an array of Person objects. When I add a new Person into the array, it is reloaded in my View, however if I change the value of an existing Person, it is not reloaded…
jarnaez
  • 1,402
  • 3
  • 10
  • 14
101
votes
10 answers

How to pass one SwiftUI View as a variable to another View struct

I'm implementing a very custom NavigationLink called MenuItem and would like to reuse it across the project. It's a struct that conforms to View and implements var body : some View which contains a NavigationLink. I need to somehow store the view…
Alex
  • 1,023
  • 2
  • 7
  • 9
101
votes
15 answers

Go to a new view using SwiftUI

I've got a basic view with a button using SwiftUI and I'm trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegate for this view that will tell the app's SceneDelegate to present a new view…
Jake
  • 13,097
  • 9
  • 44
  • 73
99
votes
6 answers

SwiftUI @Binding Initialize

Been playing around with SwiftUI and understood the concept of BindableObjects etc so far (at least I hope I do). I bumped into a stupid problem I can't seem to find an answer for: How do you initialize a @Binding variable? I have the following…
DonBaron
  • 1,354
  • 1
  • 10
  • 18
98
votes
4 answers

SwiftUI View - viewDidLoad()?

Trying to load an image after the view loads, the model object driving the view (see MovieDetail below) has a urlString. Because a SwiftUI View element has no life cycle methods (and there's not a view controller driving things) what is the best…
Kraig Wastlund
  • 1,094
  • 1
  • 8
  • 9
97
votes
12 answers

Button border with corner radius in Swift UI

I'm trying to set a rounded border to a button but the border of the button is not correct. Code: Button(action: { print("sign up bin tapped") }) { Text("SIGN UP") .frame(minWidth: 0, maxWidth: .infinity) …
Mahendra
  • 8,448
  • 3
  • 33
  • 56
96
votes
3 answers

SwiftUI: Random "Extra argument in call" error

So I'm trying to learn SwiftUI and Combine. I usually start new tech by making a simple tip calculator. I seem to be getting a random "Extra argument in call." error while coding Here is my SwiftUI File import SwiftUI internal enum ReceiptRowType…
Kwoner
  • 963
  • 1
  • 6
  • 7
96
votes
6 answers

How to detect live changes on TextField in SwiftUI?

I have a simple TextField that binds to the state 'location' like this, TextField("Search Location", text: $location) I want to call a function each time this field changes, something like this: TextField("Search Location", text: $location) { …
dylankbuckley
  • 1,507
  • 1
  • 13
  • 21
96
votes
3 answers

Difference between SceneDelegate and AppDelegate

In my SwiftUI project I see AppDelegate file as well as a SceneDelegate file. What are the differences between them? For example between the methods in SceneDelegate scene(_:willConnectTo:options:) and in the…
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
95
votes
6 answers

How to convert UIColor to SwiftUI‘s Color

I want to use a UIColor as foregroundcolor for an object but I don’t know how to convert UIColor to a Color var myColor: UIColor RoundedRectangle(cornerRadius: 5).foregroundColor(UIColor(myColor))
nOk
  • 2,725
  • 4
  • 13
  • 26