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
10
votes
2 answers

SwiftUI picker driven by an enum: value not updated

According to Apple's documentation regarding Picker in SwiftUI using an Enum, if the enum conforms to the Identifiable protocol in addition to CaseIterable, a picker iterating over all cases should update the bound variable natively. I tested it,…
alpennec
  • 1,864
  • 3
  • 18
  • 25
10
votes
1 answer

SwiftUI How to vertically center a view inside a VStack

I would like to have one view in the vertical center of the screen, one view at the top of the screen and one view vertically centered between these two views like so: This took me 5min to do on a storyboard but I don't seem to find a way to do it…
jo.On
  • 666
  • 1
  • 9
  • 13
10
votes
1 answer

How to make SwiftUI List/OutlineGroup lazy for use with large trees like a file system?

Here's a simple demo of the hierarchical List in SwiftUI. I'm testing it on macOS Big Sur, but unlike similar tree components in other UI toolkits, it asks for all its children immediately. So I can't use it for something like a file system…
Rob N
  • 15,024
  • 17
  • 92
  • 165
10
votes
2 answers

Change in Form UI in iOS 14 for SwiftUI

After building my SwiftUI for iOS 14 I've noticed that a lot of alignments were broken in forms. Now each section seems to be in a rectangle with rounded corners and extra padding. It looks great on devices with large screens, but on smaller screens…
marcgg
  • 65,020
  • 52
  • 178
  • 231
10
votes
1 answer

SwiftUI ContextMenu with Submenu

I am developing a macOS app in SwiftUI. I am using .contextMenu modifier inside my List. It all works fine, however I am trying to make a submenu. Usually I am just using buttons, I am not trying to make a hierarchy / or a submenu. It is possible…
davidev
  • 7,694
  • 5
  • 21
  • 56
10
votes
2 answers

Clear background for form sections in SwiftUI?

I'm trying to remove the white background of some sections so the elements lay right on the grey background, but I can't get the section background to be removed or transparent. This is what I'm trying: struct ContentView: View { var body:…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
10
votes
2 answers

SwiftUI Nested ForEach causes Fatal error: each layout item may only occur once

I'm trying to create a LazyVGrid view to display the contents of objects in an array using nested ForEach statements. The code is causing an app crash with the message "Fatal error: each layout item may only occur once". Each object contains an…
tommy
  • 277
  • 2
  • 8
10
votes
1 answer

Catching errors in SwiftUI

I have a button in some view that calls a function in the ViewModel that can throw errors. Button(action: { do { try self.taskViewModel.createInstance(name: self.name) } catch DatabaseError.CanNotBeScheduled { Alert(title:…
ChesterK2
  • 320
  • 3
  • 11
10
votes
1 answer

Fill circle with wave animation in SwiftUI

I have created a circle in swiftUI, and I want to fill it with sine wave animation for the water wave effect/animation. I wanted to fill it with a similar look: Below is my code: import SwiftUI struct CircleWaveView: View { var body: some View…
ItsZiaW
  • 180
  • 2
  • 14
10
votes
1 answer

What is the role of tag in SwiftUI?

I'm confused of role of tag. When I remove tag, I can pick only "M", and add, I can pick all options. When I made Integer array picker, the presence of tag don't matter at all. But The problem occurs in String array. And I want to know why this…
lgm0905
  • 125
  • 1
  • 5
10
votes
3 answers

App crashes with [ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke

My App crashes randomly with this error: [ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke (didReceiveMemoryWarning fires before the error message) It is a bit hard to post code because of…
Peter Pohlmann
  • 1,478
  • 15
  • 30
10
votes
3 answers

Can I add WidgetKit extensions to an app with target iOS < 14?

I want to add a (WidgetKit-)widget to my iOS app. The app itself should keep a target of < 14. Obviously, the widget would be available only on those devices >= 14, but the app should run on all other devices as well. Is this possible?
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
10
votes
3 answers

How can I vertically align an image to the center of the first line of some text?

I have a bullet point and some long, multiline text. I want the bullet point to be aligned with the center of the first line of text. Obviously, If the string is sufficiently short and one line long then the two views are automatically center…
DanubePM
  • 1,526
  • 1
  • 10
  • 24
10
votes
0 answers

SwiftUI - macOS Big Sur - How to move ToolbarItem to corresponding right view

I'm trying to create a three column window with a sidebar and a splitview. Each of the left and right views in the split view should have a corresponding ToolbarItem that display in their respective toolbar/navigationtitle space. For this purpose, I…
Felix Marianayagam
  • 2,634
  • 2
  • 9
  • 29
10
votes
2 answers

Align text as leading in ScrollView - SWIFTUI

I have a ScrollView: ScrollView { VStack (alignment: .leading) { Text("\(userData.username), \(userData.age)") .fontWeight(.semibold) .font(Font.system(size:30)) } } The output…
Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62