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
50
votes
12 answers

How change background color if using NavigationView in SwiftUI?

I want to change background color for full screen. I am using NavigationView, and I want to set Gray color for background (not default white) struct ContentView : View { var body: some View { NavigationView { ScrollView { …
Ivan Vorobei
  • 629
  • 1
  • 6
  • 6
50
votes
10 answers

How to have a dynamic List of Views using SwiftUI

I can do a static List like List { View1() View2() } But how do i make a dynamic list of elements from an array? I tried the following but got error: Closure containing control flow statement cannot be used with function builder…
Just a coder
  • 15,480
  • 16
  • 85
  • 138
50
votes
8 answers

How to remove "row" separators/dividers from a List in SwiftUI?

I'm trying to remove the "row" separators (known as dividers in SwiftUI) from a List in SwiftUI. I went through the List documentation, but I haven't been able to find a modifier for that. Any help would be appreciated.
Schiopu Evgheni
  • 549
  • 1
  • 4
  • 13
50
votes
3 answers

What's with Constraints in SwiftUI?

What is happening now with constraints in SwiftUI? Do View types adapt automatically for bigger devices etc. or what should we have to do instead?
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
49
votes
4 answers

SwiftUI DatePicker jumps between short and medium date formats when changing the date

(Using Xcode 12.3 and iOS 14.3) I use a DatePicker component with the default style (DefaultDatePickerStyle) to display and edit a date: the component shows the current date value in a label and pops up a calendar when tapping that label. So far, so…
dr_barto
  • 5,723
  • 3
  • 26
  • 47
49
votes
29 answers

SwiftUI / Xcode Error - Updating took more than 5 seconds

When I try to preview one of my views in the canvas I keep getting the following error: PreviewUpdateTimedOutError: Updating took more than 5 seconds All my other views load perfectly fine. Why is this happening and how do I resolve? I am using…
MattBlack
  • 3,616
  • 7
  • 32
  • 58
49
votes
4 answers

SwiftUI - Invert a boolean binding

I have a Binding binding in a swiftUI view. Something along the lines of: struct MyCoolView: View { @ObservedObject var viewModel: ViewModel var body: some View { Text("Here is a cool Text!").sheet(isPresented:…
snarik
  • 1,035
  • 2
  • 9
  • 15
49
votes
7 answers

SwiftUI ButtonStyle - how to check if button is disabled or enabled?

To style a button in SwiftUI, according to my understanding, you extend ButtonStyle and implement func makeBody(configuration: Self.Configuration) -> some View within which you start applying modifications to the configuration.label which is a…
hasen
  • 161,647
  • 65
  • 194
  • 231
49
votes
1 answer

SwiftUI Views with a custom init

Let's say that I am making a custom input element that looks like this: struct CustomInput : View { @Binding var text: String var name: String var body: some View { TextField(name, text: $text) …
bento
  • 4,846
  • 8
  • 41
  • 59
49
votes
3 answers

How can I preview a device in landscape mode in SwiftUI?

How can I preview a device in landscape mode in SwiftUI? I just have a simple preview like this: struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } }
Felipe Peña
  • 2,712
  • 4
  • 22
  • 41
49
votes
5 answers

SwiftUI view is in the middle instead of in the top

I'm trying to create a view in SwiftUI. In the preview, it looks like it should, but when run on my iPhone (or on the live preview) it looks like it is offset. I tried to set the padding to -150, but then the TextField doesn't respond to…
Shahar Melamed
  • 1,534
  • 5
  • 15
  • 23
48
votes
6 answers

How to bold text in SwiftUI TextField?

There is no built-in font-weight modifier for textfield in SwiftUI, as of Xcode 11.2.1. How can we introduce font-weight without extending UITextField as UIViewRepresentable?
Abhishek Biswas
  • 1,125
  • 1
  • 13
  • 19
48
votes
10 answers

How can I have two alerts on one view in SwiftUI?

I want to have two unique alerts attached to the same Button view. When I use the code below, only the alert on the bottom works. I'm using the official release of Xcode 11 on macOS Catalina. @State private var showFirstAlert = false @State private…
Luke Chambers
  • 785
  • 1
  • 10
  • 16
48
votes
5 answers

How to conform an enumeration to Identifiable protocol in Swift?

I'm trying to make a list with the raw values of the cases from an enumeration with the new SwiftUI framework. However, I'm having a trouble with conforming the 'Data' to Identifiable protocol and I really cannot find information how to do it. It…
48
votes
0 answers

SwiftUI UITableView was told to layout its visible cells and other contents without in view hierarchy

I have a list in a view (lets call it view A) where I push a view B via NavigationLink from view A and manipulate the CurrentSubjectValue to the initial view A. The moment the data is added and the didChangd.send(value) is dispatched to view A I get…
Congruent Tech. UG
  • 1,408
  • 2
  • 12
  • 21