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
1 answer

Button to open view in new window SwiftUI 5.3 for Mac OS X

I would like to have a button to open a new window and load a view (for the app's Preferences) in SwiftUI for MacOS but am unsure of the correct way to do it. I tried creating a function and calling it from the button action which works but on…
mousebat
  • 474
  • 7
  • 25
10
votes
2 answers

How to observe changes in UserDefaults?

I have an @ObservedObject in my View: struct HomeView: View { @ObservedObject var station = Station() var body: some View { Text(self.station.status) } which updates text based on a String from Station.status: class Station:…
Zorgan
  • 8,227
  • 23
  • 106
  • 207
10
votes
1 answer

Instance method 'appendInterpolation' requires that 'Decimal' conform to '_FormatSpecifiable' when attempting to use in SwiftUI

import Foundation import SwiftUI struct SampleComponent: View { @Binding var value: Decimal var body: some View { return Text("\(value)") } } Gives me the error: Instance method 'appendInterpolation' requires that 'Decimal' conform to…
Senseful
  • 86,719
  • 67
  • 308
  • 465
10
votes
3 answers

iOS 14 Widgets + SwiftUI + Firebase?

I'm still pretty new to SwiftUI and Firebase. Recently, as a hobby, I have been developing an app for my school. After the launch of Xcode 12, I decided to experiment with the new features such as Widgets. However, since my app gets its data from…
Daniel Ho
  • 131
  • 1
  • 7
10
votes
2 answers

Can SwiftUI 2.0 new features introduced in WWDC 2020 run on iOS 13?

SwiftUI has new features for Grid lists with lazy loading and PageView that I'm interested in implementing in my existing SwiftUI app compatible with iOS 13. Let's say I compile my app in Xcode 12 and add these new codes, will it be compatible with…
Filipe Sá
  • 372
  • 1
  • 5
  • 15
10
votes
4 answers

image not appearing in SwiftUI

Hello I am new in SwiftUI, I am working with the tag Image("my image"), my image is on the directory Assets.xcassets everything okay when I am editing but when I am testing with my cell or my simulator in my mac the image not appear and I don´t…
10
votes
1 answer

why can't I override a method that returns some View?

In the following example import SwiftUI class AbstractOverride { open func configurationView() -> AnyView { if Features.TEST_VERSION { return AnyView(Text("override configurationView()")) } else { return…
Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
10
votes
2 answers

SwiftUI Circular Stroke Gradient

I have implemented a circular stroke as you can see in the picture below (of course without the numbers ). Unfortunately, I encounter two problems that you may be able to help me with. I want the gradient to go along the line, i.e. the darkest…
Phillipp
  • 312
  • 3
  • 12
10
votes
3 answers

Shadows clipped by ScrollView

Is there a way I can get shadows applied to an image to also show outside of the parent view? Below you can see a sample, where I list out some images in a ScrollView and HStack. The image/artwork has a shadow applied to it, but this gets clipped by…
infero
  • 300
  • 2
  • 11
10
votes
6 answers

Highlight a specific part of the text in SwiftUI

Hello I'm new to Swift and am using SwiftUI for my project where I download some weather data and I display it in the ContentView(). I would like to highlight some part of the Text if it contains some specific word, but I don't have any idea how to…
Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
10
votes
3 answers

Difference between Buttons and onTapGesture

I’m quite new to swiftui and I was curious on what’s the difference between using a button displaying an image or an image with onTapGesture modifier that make an action just like the action on buttons.
xmetal
  • 681
  • 6
  • 16
10
votes
1 answer

Core Data and SwiftUI polymorphism issues

Having troubles putting down together SwiftUI and generic types for handling Core Data. Consider following example: Parent is abstract. Foo and Bar are children of Parent and they have some custom attributes. Now what I want to do, is roughly…
Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31
10
votes
5 answers

Unwanted animation when moving items in SwiftUI list

I have a SwiftUI List like in the example code below. struct ContentView: View { @State var numbers = ["1", "2", "3"] @State var editMode = EditMode.inactive var body: some View { NavigationView { List { …
Mateusz K
  • 175
  • 1
  • 5
10
votes
1 answer

How to get the index of a deleted row from a list in SwiftUI?

I want to delete an element from an array that I am displaying as a list using a ForEach, but I also need to send a HTTP request to a REST API and I need to put the index of the element in the body of the request. Here is my…
Susca Bogdan
  • 991
  • 1
  • 9
  • 23
10
votes
1 answer

Is there a way to have a global variable between a SwiftUI view and classes indirectly related to it?

I am working on a MacOS Swift project (with SwiftUI), and I am having trouble with updating a text label as an event happens. struct ContentView: View { @State var now = globalString.stringy var body: some View { VStack { …
makertech81
  • 898
  • 2
  • 7
  • 20