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

UITextView - adjust size based on the content in SwiftUI

I am trying to figure out how to make UITextView size dependent on it's contents in SwiftUI. I wrapped the UITextView in UIViewRepresentable as following: struct TextView: UIViewRepresentable { @Binding var showActionSheet: Bool func…
Adam
  • 1,054
  • 1
  • 12
  • 26
11
votes
2 answers

How to dynamically create sections in a SwiftUI List/ForEach and avoid "Unable to infer complex closure return type"

I'm trying to recreate a view that matches the event list in the stock iOS Calendar app. I have the following code which generates a list of events with each event separated into its own section by Date: var body: some View { NavigationView { …
Spielo
  • 481
  • 1
  • 6
  • 17
11
votes
3 answers

Create a NavigationLink without back button SwiftUI

Im trying to link a button action in SomeView1() to navigate to a someView2() without having the back button at the top of the screen. Instead, I want to add another button in SomeView2() that will navigate back to SomeView1(). is this possible in…
Liv
  • 235
  • 3
  • 9
11
votes
5 answers

SwiftUI Text clips on resize

In the following sample, tapping the 'Expand' button causes the text '39' to clip as it resizes in transition to '40'. The real context of this use is a text label that reflects the value of a picker in an animated diagram. I'd like for the text to…
Michael Pangburn
  • 261
  • 1
  • 10
11
votes
2 answers

SwiftUI Font scaling in a complex View

My goal is to make sure Text in a container to scale according to its parent. It works well when the container only contains one Text view, as following: import SwiftUI struct FontScalingExperiment: View { var body: some View { …
wk.experimental
  • 523
  • 4
  • 9
11
votes
2 answers

How to right-align item labels in a custom SwiftUI form on AppKit?

I have the following Cocoa form: struct Canvas: PreviewProvider { static var previews: some View { VStack { HStack(alignment: .firstTextBaseline) { Text("Endpoint:") TextField("https://localhost:8080/api", text:…
Vadim
  • 9,383
  • 7
  • 36
  • 58
11
votes
3 answers

Calling a local UIViewController function from a SwiftUI View

I'm trying to call a local ViewController function from ContentView. The function uses some local variables and cannot be moved outside the ViewController. class ViewController: UIViewController { func doSomething() {...} } extension…
Liv
  • 235
  • 3
  • 9
11
votes
4 answers

How to trigger a function when a Slider changes in SwiftUI?

I'm trying to trigger the sliderchanged() function when the slider value changes, I might be missing something very basic but every similar thing I've found isn't working. Isn't there anything like "action" for the Slider or the old "ValueChanged"…
Gianclgar
  • 113
  • 1
  • 6
11
votes
2 answers

Unit testing in SwiftUI

I am trying to write unit tests for SwiftUI views but finding zero resources on the web for how to go about that. I have a view like the following struct Page: View { @EnvironmentObject var service: Service var body: some View { NavigationView…
user1366265
  • 1,306
  • 1
  • 17
  • 28
11
votes
3 answers

Mask rectangle with a text in SwiftUI

I want to achieve following using SwiftUI: This is what I have tried: Text("test").mask(Rectangle().frame(width: 200, height: 100).foregroundColor(.white)) Also the other way around: Rectangle().frame(width: 200, height:…
ph1psG
  • 568
  • 5
  • 24
11
votes
3 answers

'init(systemName:)' is unavailable in macOS

I would like to use SF Symbols in my macOS project. How to implement one? Button(action: {}) { Image(systemName: "star") //Error: 'init(systemName:)' is unavailable in macOS }
Victor Kushnerov
  • 3,706
  • 27
  • 56
11
votes
3 answers

Cannot use mutating member on immutable value: 'self' is immutable in SwiftUI

For the following code, I am getting the following error. I don't know how to work around this. How can I call volumeCheck() upon the button click? struct ContentView: View { var player = AVAudioPlayer() var body: some View { …
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
11
votes
3 answers

Converting from UIImage to a SwiftUI Image results in a blank image of the same size

I am trying to convert a UIImage to a SwiftUI Image using the init(uiImage:) initializer. My UIImage itself is created from a CIImage generated by a CIQRCodeGenerator CIFilter. I am running my code on a Playground in Xcode 11.1 GM seed 1. Here is…
Eugene
  • 3,417
  • 5
  • 25
  • 49
11
votes
2 answers

SwiftUI - Making a View focusable and trigger an action on macOS

How is it possible to make a View on macOS focusable. I tried the it like below but the action is never triggered. I know that for NSView you have to implement acceptsFirstResponder to return true but can not find similar in SwiftUI. Is this still a…
Marc T.
  • 5,090
  • 1
  • 23
  • 40
11
votes
1 answer

Supporting SwiftUI navigation in legacy projects

I'm wondering if there's any way to start using SwiftUI in a legacy project and still be able to navigate back and forth SwiftUI scenes. That is, let's imagine I just want to support the last iOS 13 for a project I've been working on for the last…
Fernando
  • 751
  • 2
  • 13
  • 27