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
56
votes
8 answers

SwiftUI NavigationView on the iPad Pro

I use the Apple SwiftUI tutorial code. Then I set the previewDevice to iPad Pro (12.9-inch). But the preview has something wrong. Does anyone know where the problem is?
Jerry Lee
  • 823
  • 2
  • 7
  • 13
56
votes
9 answers

SwiftUI TextField with formatter not working?

I'm trying to get a numeric field updated so I'm using a TextField with the formatter: parameter set. It formats the number into the entry field just fine, but does not update the bound value when edited. The TextField works fine (on Strings)…
KRH
  • 766
  • 1
  • 6
  • 8
56
votes
16 answers

Pull down to refresh data in SwiftUI

i have used simple listing of data using List. I would like to add pull down to refresh functionality but i am not sure which is the best possible approach. Pull down to refresh view will only be visible when user tries to pull down from the very…
PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
55
votes
9 answers

How to detect Swiping UP, DOWN, LEFT and RIGHT with SwiftUI on a View

I'm getting into building Apple Watch apps. What I'm currently working on will require me to make use of detecting swipes in the four main directions (UP, DOWN, LEFT and RIGHT) The problem is I have no idea how to detect this. I've been looking…
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
55
votes
7 answers

NavigationView and NavigationLink on button click in SwiftUI?

I am trying to push from login view to detail view but not able to make it.even navigation bar is not showing in login view. How to push on button click in SwiftUI? How to use NavigationLink on button click? var body: some View { …
Rock
  • 1,408
  • 1
  • 12
  • 27
54
votes
12 answers

How to make hyperlinks in SwiftUI

In Swift, as shown here, you can use NSMutableAttributedString to embed links in text. How can I achieve this with SwiftUI? I implemented it as the following, but it does not look how I want it to. . import SwiftUI struct ContentView: View { …
Ika
  • 1,271
  • 1
  • 12
  • 20
54
votes
5 answers

How do you create a multi-line text inside a ScrollView in SwiftUI?

Since List doesn't look like its configurable to remove the row dividers at the moment, I'm using a ScrollView with a VStack inside it to create a vertical layout of text elements. Example below: ScrollView { VStack { // ... …
George Ananda Eman
  • 3,292
  • 8
  • 28
  • 29
53
votes
7 answers

SwiftUI | Using onDrag and onDrop to reorder Items within one single LazyGrid?

I was wondering if it is possible to use the View.onDrag and View.onDrop to add drag and drop reordering within one LazyGrid manually? Though I was able to make every Item draggable using onDrag, I have no idea how to implement the dropping…
Kai Zheng
  • 6,640
  • 7
  • 43
  • 66
53
votes
0 answers

SwiftUI warning: `Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state`

Using Xcode 12 beta (12A6159) on macOS Catalina 10.15.5 (19F101) when I press "Back" button in NavigationView, navigating back from a pushed View, I see this warning in the console. [UIContextMenuInteraction] Attempting…
Sajjon
  • 8,938
  • 5
  • 60
  • 94
53
votes
6 answers

What is PassthroughSubject & CurrentValueSubject

I happen to look into Apple's new Combine framework, where I see two things PassthroughSubject CurrentValueSubject Can someone explain to me what is meaning & use of them?
Nasir
  • 1,617
  • 2
  • 19
  • 34
53
votes
3 answers

SwiftUI preview provider with binding variables

How do I generate a preview provider for a view which has a binding property? struct AddContainer: View { @Binding var isShowingAddContainer: Bool var body: some View { Button(action: { self.isShowingAddContainer =…
53
votes
6 answers

Is it possible for a NavigationLink to perform an action in addition to navigating to another view?

I'm trying to create a button that not only navigates to another view, but also run a function at the same time. I tried embedding both a NavigationLink and a Button into a Stack, but I'm only able to click on the Button. ZStack { …
Jnguyen22
  • 967
  • 2
  • 8
  • 13
53
votes
13 answers

Dismiss a SwiftUI View that is contained in a UIHostingController

I have rewritten my sign in view controller as a SwiftUI View. The SignInView is wrapped in a UIHostingController subclass (final class SignInViewController: UIHostingController {}), and is presented modally, full screen, when sign in is…
jjatie
  • 5,152
  • 5
  • 34
  • 56
53
votes
11 answers

How to detect a tap gesture location in SwiftUI?

(For SwiftUI, not vanilla UIKit) Very simple example code to, say, display red boxes on a gray background: struct ContentView : View { @State var points:[CGPoint] = [CGPoint(x:0,y:0), CGPoint(x:50,y:50)] var body: some View { return…
Epaga
  • 38,231
  • 58
  • 157
  • 245
53
votes
6 answers

What are the .primary and .secondary colors in SwiftUI?

In the new SwiftUI, the type Color is very similar to UIColor from UIKit. There are the common colors, as expected, but there is the addition of two other colors that I noticed: .primary .secondary There is nothing in the Apple Documentation for…
George
  • 25,988
  • 10
  • 79
  • 133