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
173
votes
20 answers

Add bottom border line to UI TextField view in SwiftUI / Swift / Objective-C / Xamarin

I would like to keep the border at the bottom part only in UITextField. But I don't know how we can keep it on the bottom side. Can you please advise me?
dhaval shah
  • 4,499
  • 10
  • 29
  • 42
171
votes
22 answers

SwiftUI: How to make TextField become first responder?

Here's my SwiftUI code: struct ContentView : View { @State var showingTextField = false @State var text = "" var body: some View { return VStack { if showingTextField { TextField($text) …
Epaga
  • 38,231
  • 58
  • 157
  • 245
165
votes
6 answers

Opt out of UISceneDelegate/SwiftUI on iOS

I'm currently using Xcode 11 Beta 5. Within my application, it runs fine on iOS 12 and under. However, on iOS 13 it looks like it's using the UIScene by default. This is causing my app to not do anything. When the app launches on fresh install,…
DavidA
  • 1,809
  • 3
  • 13
  • 12
165
votes
23 answers

How to remove the line separators from a List in SwiftUI without using ForEach?

I have this code to display a list of custom rows. struct ContentView : View { var body: some View { VStack(alignment: .leading) { List(1...10) {_ in CustomRow() } } } } However, I…
ygee
  • 2,580
  • 3
  • 17
  • 21
159
votes
21 answers

Multiple sheet(isPresented:) doesn't work in SwiftUI

I have this ContentView with two different modal views, so I'm using sheet(isPresented:) for both, but as it seems only the last one gets presented. How could I solve this issue? Or is it not possible to use multiple sheets on a view in…
turingtested
  • 6,356
  • 7
  • 32
  • 47
157
votes
11 answers

Include SwiftUI views in existing UIKit application

Is is possible to build views with SwiftUI side by side with an existing UIKit application? I have an existing application written in Objective-C. I've begun migrating to Swift 5. I'm wondering if I can use SwiftUI alongside my existing UIKit .xib…
visc
  • 4,794
  • 6
  • 32
  • 58
154
votes
11 answers

iOS SwiftUI: pop or dismiss view programmatically

I couldn't find any reference about any ways to make a pop or a dismiss programmatically of my presented view with SwiftUI. Seems to me that the only way is to use the already integrated slide dow action for the modal(and what/how if I want to…
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
153
votes
12 answers

Is SwiftUI backwards-compatible with iOS 12.x and older?

If I have an app made with SwiftUI, will it work for iOS below iOS 13?
Ted
  • 22,696
  • 11
  • 95
  • 109
148
votes
12 answers

Initialize @StateObject with a parameter in SwiftUI

I would like to know if there is currently (at the time of asking, the first Xcode 12.0 Beta) a way to initialize a @StateObject with a parameter coming from an initializer. To be more specific, this snippet of code works fine: struct MyView: View…
Emilio Schepis
  • 1,677
  • 2
  • 9
  • 11
148
votes
12 answers

Dynamically hiding view in SwiftUI

I'm trying to conditionally hide a DatePicker in SwiftUI. However, I'm having any issue with mismatched types: var datePicker = DatePicker($datePickerDate) if self.showDatePicker { datePicker = datePicker.hidden() } In this case, datePicker is…
Jake
  • 13,097
  • 9
  • 44
  • 73
145
votes
29 answers

Move TextField up when the keyboard has appeared in SwiftUI

I have seven TextField inside my main ContentView. When user open keyboard some of the TextField are hidden under the keyboard frame. So I want to move all TextField up respectively when the keyboard has appeared. I have used the below code to add…
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
138
votes
14 answers

SwiftUI - how to avoid navigation hardcoded into the view?

I try to do the architecture for a bigger, production ready SwiftUI App. I am running all the time into the same problem which points to a major design flaw in SwiftUI. Still nobody could give me a full working, production ready answer. How to do…
Darko
  • 9,655
  • 9
  • 36
  • 48
138
votes
10 answers

How to remove the left and right Padding of a List in SwiftUI?

How to remove the left and right Padding of a List in SwiftUI? Every List i create has borders to the leading and trailing of a cell. What modifier should I add to remove this?
Just a coder
  • 15,480
  • 16
  • 85
  • 138
138
votes
15 answers

Conditionally use view in SwiftUI

I'm trying to figure out the correct way to conditionally include a view with swiftui. I wasn't able to use the if directly inside of a view and had to use a stack view to do it. This works but there seems like there would be a cleaner way. var…
Michael St Clair
  • 5,937
  • 10
  • 49
  • 75
137
votes
7 answers

SwiftUI app life cycle iOS14 where to put AppDelegate code?

Now that AppDelegate and SceneDelegate are removed from SwiftUI, where do I put the code that I used to have in SceneDelegate and AppDelegate, Firebase config for ex? So I have this code currently in my AppDelegate: Where should I put this code…
M1X
  • 4,971
  • 10
  • 61
  • 123