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
45
votes
3 answers

Convert from SwiftUI.Font to UIFont

I have a UIViewRepresentable with a UITextView and I want to set the font on the UITextView using the current Font from the SwiftUI environment. I just need a simple init like: UIFont(_ swiftUIFont: Font) But I can't find any such thing. And the…
Richard Venable
  • 8,310
  • 3
  • 49
  • 52
45
votes
7 answers

Hide navigation bar without losing swipe back gesture in SwiftUI

In SwiftUI, whenever the navigation bar is hidden, the swipe to go back gesture is disabled as well. Is there any way to hide the navigation bar while preserving the swipe back gesture in SwiftUI? I've already had a custom "Back" button, but still…
Nguyễn Khắc Hào
  • 1,980
  • 2
  • 15
  • 25
45
votes
5 answers

Is there a SwiftUI equivalent for viewWillDisappear(_:) or detect when a view is about to be removed?

In SwiftUI, I'm trying to find a way to detect that a view is about to be removed only when using the default navigationBackButton. Then perform some action. Using onDisappear(perform:) acts like viewDidDisappear(_:), and the action performs after…
FRIDDAY
  • 3,781
  • 1
  • 29
  • 43
45
votes
6 answers

SwiftUI @Binding update doesn't refresh view

I feel like I'm missing something very basic, but this example SwiftUI code will not modify the view (despite the Binding updating) when the button is clicked Tutorials I have read suggest this is the correct way to use a binding and the view should…
simeon
  • 4,466
  • 2
  • 38
  • 42
45
votes
4 answers

Binding value from an ObservableObject

Aim: I have a model which is an ObservableObject. It has a Bool property, I would like to use this Bool property to initialise a @Binding variable. Questions: How to convert an @ObservableObject to a @Binding ? Is creating a @State the only way to…
user1046037
  • 16,755
  • 12
  • 92
  • 138
45
votes
8 answers

Change the stroke/fill color of SF Symbol icon in SwiftUI?

I am looking for a way to change the stroke / fill color of an SF Symbol icon in SwiftUI. I have tried .background(Color.red) but that just changes the background of the whole icon (no change is applied to the actual icon itself) as implied. I also…
45
votes
9 answers

How to open the ImagePicker in SwiftUI?

I need to open an image picker in my app using SwiftUI, how can I do that? I thought about using the UIImagePickerController, but I don't know how to do that in SwiftUI.
Shahar Melamed
  • 1,534
  • 5
  • 15
  • 23
45
votes
10 answers

Remove extra line separators below List in SwiftUI

I've created a simple List as below, but there are extra separators below it. List { Text("Item 1") Text("Item 2") Text("Item 3") } Result: I've tried embedding the List inside a VStack and adding Spacer() like below code but it's not…
M Reza
  • 18,350
  • 14
  • 66
  • 71
44
votes
3 answers

iPhone is not available. Please reconnect the device. / iOS 14.2 - Xcode 12.1

I just updated my iPhone to iOS 14.2. When I want to launch an app on the device from Xcode, I now keep seeing this: I also just updated Xcode to Version 12.1, hoping it would solve the problem, but it doesn't make any difference, I keep getting…
Michel
  • 10,303
  • 17
  • 82
  • 179
44
votes
4 answers

Use Binding with a TextField SwiftUI

I am currently building a page to add player information to a local database. I have a collection of TextFields for each input which is linked to elements in a player struct. var body: some View { VStack { TextField("First Name",…
Bernard
  • 1,004
  • 1
  • 12
  • 21
44
votes
21 answers

SwiftUI NavigationLink Hide Arrow

Is there a way to hide the arrow to the right of the navigation link view that is automatically added? I want to show an image grid using NavigationView -> List -> HStack -> NavigationLink_1 - NavigationLink_2 The NavigationLinks have arrows and it…
blackops
  • 2,205
  • 2
  • 18
  • 22
44
votes
13 answers

How to show HTML or Markdown in a SwiftUI Text?

How can I set a SwiftUI Text to display rendered HTML or Markdown? Something like this: Text(HtmlRenderedString(fromString: "Hi!")) or for MD: Text(MarkdownRenderedString(fromString: "**Bold**")) Perhaps I need a different View?
Div
  • 1,363
  • 2
  • 11
  • 28
44
votes
1 answer

SwiftUI: Putting multiple BindableObjects into Environment

In SwiftUI it is possible to use the environmentObject method of the View object to put a single BindableObject into the environment. What if I want to put multiple BindableObjects at the same time into the environment? I don't see any solution for…
ideveloper
  • 473
  • 1
  • 4
  • 10
44
votes
7 answers

Set the Navigation Bar Title Font with SwiftUI

This is a SwiftUI question, not a UIKit one. :) I'm trying to set a different font for the navigation bar title using SwiftUI. My suspicion is that this isn't supported yet. Here's what I've tried: var body: some View { NavigationView { …
Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
43
votes
6 answers

How to replace deprecated .animation() in SwiftUI?

The .animation() modifier has been deprecated in iOS 15, but I'm not sure I understand how Xcode's suggested equivalent, animation(_:value:), works. .animation(.easeInOut(duration: 2)) // ⚠️'animation' was deprecated in iOS 15.0: Use withAnimation…
Sam
  • 2,152
  • 6
  • 31
  • 44