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

How to implement a custom property wrapper which would publish the changes for SwiftUI to re-render it's view

Trying to implement a custom property wrapper which would also publish its changes the same way @Publish does. E.g. allow my SwiftUI to receive changes on my property using my custom wrapper. The working code I have: import…
Pavel Lobodinský
  • 1,028
  • 1
  • 12
  • 25
10
votes
3 answers

Returning a populated VStack from a SwiftUI function

I would like to return different views from a function, a Text or a VStack of a Text and a Button. Here's one of my attempts: func buildResponseText2() -> some View { if (userData.success) { return VStack { Text("Well Done") …
dumbledad
  • 16,305
  • 23
  • 120
  • 273
10
votes
2 answers

Reference to member 'default' cannot be resolved without a contextual type

This error occurs with this code and I have no idea what it means: .alert(isPresented: $showingAlert) { Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("OK"))) Any help please.
Michael Mouer
  • 127
  • 1
  • 2
  • 8
10
votes
3 answers

Bringing up the Google Signin Screen in SwiftUI

I am playing around with the Google sign in screen and swiftUI, but I keep getting a bunch of messages in my logs that I don't really understand - now the logs I can live with, but the problem I am having is that I can't call the same function…
jwknz
  • 6,598
  • 16
  • 72
  • 115
10
votes
2 answers

SwiftUI Form Picker only shows once

I am playing around with SwiftUI, and am currently building a Form using a Picker. import SwiftUI struct ContentView: View { private let names = ["Bill", "Peter", "Johan", "Kevin"] @State private var favoritePerson = "Bill" var body: some…
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
10
votes
1 answer

How to play audio using AVAudioPlayer in SwiftUI project

Audio is not playing using this approach. The play() function is executing without any error. Please help var audioPlayer = AVAudioPlayer() let path = Bundle.main.path(forResource: "a", ofType: "mp3") @State var isPlaying : Bool = false var body:…
deepak joshi
  • 125
  • 1
  • 9
10
votes
4 answers

SwiftUI Navigation Bar and Status Bar - Make them same color

I have a new app that ideally will have a solid dark blue navigation bar at the top that extends up behind the status bar. It was a pain to make this opaque and the correct color of blue, but I finally figured out how to make it work by putting the…
eResourcesInc
  • 948
  • 1
  • 9
  • 17
10
votes
2 answers

Property type 'Int' does not match that of the 'wrappedValue' property of its wrapper type 'EnvironmentObject'

I'm trying to use an @EnvironmentObject to pass data to my SwiftUI view: struct MyView: View { @EnvironmentObject var myInt: Int // ❌ Property type 'Int' does not match that of the 'wrappedValue' property of its wrapper type…
Senseful
  • 86,719
  • 67
  • 308
  • 465
10
votes
3 answers

Is there a way to modify fetched results with a predicate after they are initialized?

I am trying to build a search view for an existing CoreData app (simple logging app). I have all the data stored with CoreData and is fetched with the @FetchRequest: @State private var searchPredicate: NSPredicate? = NSPredicate(format: "title…
Jannis
  • 175
  • 10
10
votes
1 answer

SwiftUI memory leak when referencing property from closure inside Form/NavigationView and swapping views

I have something like this: struct SomeView: View { @ObservedObject var viewModel: SomeViewModel var body: some View { NavigationView { // <- culprit Button(action: { self.viewModel.logOut() }) { Text("X").frame(width: 40, height:…
NeverwinterMoon
  • 2,363
  • 21
  • 24
10
votes
2 answers

How to implement a List of TextFields in SwiftUI without breaking deletion

When I Create a Master-Detail App in XCode Use Core Data Add a new field to the Event Model (say title as String) Change the MasterView implementation to this struct MasterView: View { @FetchRequest( sortDescriptors:…
Holger
  • 1,648
  • 1
  • 16
  • 26
10
votes
1 answer

SwiftUI - Large navigation bar is broken when a background color is set

I'm running into a very weird bug and couldn't figure out how to work around it. I have a navigation view with a navigation title. I noticed that the moment I change the background color of its containing view, the navigation bar overlaps with the…
user1366265
  • 1,306
  • 1
  • 17
  • 28
10
votes
1 answer

How to refresh swiftUI view automatically after Core Data updating in background?

How to refresh swiftUI view automatically after Core Data updating in background, without onAppear method being called? struct Page1: View { @FetchRequest(entity: Word.entity(), sortDescriptors: []) var wordsFromCoreData:…
Braver Chiang
  • 351
  • 2
  • 13
10
votes
2 answers

Swiftui Form View jump up bug

I had a problem when following Raywenderlich Tutorial. The form view will automatically jump upward a little bit every time i clicked an item. Is this a bug? Is there anyway to solve it? The code is below. And in Contentview, i used a…
Dawei Xu
  • 314
  • 1
  • 11
10
votes
1 answer

SwiftUI updates reduce FPS of metal window

I'm experimenting with SwiftUI and Metal. I've got 2 windows, one with various lists and controls and the other a Metal window. I had the slider data updating the Metal window but when I moved the slider the FPS dropped from 60 to around 25. I…
Leigh
  • 188
  • 2
  • 13
1 2 3
99
100