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
48
votes
4 answers

SwiftUI Image clipsToBounds

Experimenting with SwiftUI (Xcode 11.0 beta 2), I try to fill a View with an image : Image("large") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 80, height: 80, alignment: .center) .border(Color.black) This renders…
Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
48
votes
5 answers

Changing the color of a button in SwiftUI based on disabled or not

I have a textfield with a send button that's a systemImage arrow. I want the foreground color of the image to change depending on whether the textField is empty or not. (I.e. the button is gray, and it is disabled if the textfield is empty. It's…
mg_berk
  • 731
  • 2
  • 8
  • 12
48
votes
5 answers

How can I change background color of button in SwiftUI?

What is the best way to change the button's background color in SwiftUI? I want to create a tile which is a button with centered Text and rectangular background of some color.
wictorious
  • 851
  • 3
  • 10
  • 24
47
votes
3 answers

How to do a "reveal"-style collapse/expand animation in SwiftUI?

I'd like to implement an animation in SwiftUI that "reveals" the content of a view to enable expand/collapse functionality. The content of the view I want to collapse and expand is complex: It's not just a simple box, but it's a view hierarchy of…
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
47
votes
2 answers

Accessing StateObject's object without being installed on a View. This will create a new instance each time - SwiftUI

I have to do some initial setup each time the app starts, but I'm getting the error: The error is clear, the answer is not. I tried putting the init in a subview, but I can't, it needs to be in the root @main. This is how I have it…
Arturo
  • 3,254
  • 2
  • 22
  • 61
47
votes
6 answers

Get the current scroll position of a SwiftUI ScrollView

With the new ScrollViewReader, it seems possible to set the scroll offset programmatically. But I was wondering if it is also possible to get the current scroll position? It seems like the ScrollViewProxy only comes with the scrollTo method,…
Kai Zheng
  • 6,640
  • 7
  • 43
  • 66
47
votes
7 answers

How can I implement PageView in SwiftUI?

I am new to SwiftUI. I have three views and I want them in a PageView. I want to move each Views by swipe like a pageview and I want the little dots to indicate in which view I'm in.
Azhagusundaram Tamil
  • 2,053
  • 3
  • 21
  • 36
47
votes
5 answers

How to create a color with an alpha value using SwiftUI?

In UIKit to create a color with an alpha value there are different ways, for example: UIColor(red: 0, green: 0, blue: 0, alpha: 0.8) but this method is not available on the corresponding class Color of SwiftUI. What can I use?
IgnazioC
  • 4,554
  • 4
  • 33
  • 46
47
votes
5 answers

SwiftUI - Two buttons in a List

I've got two buttons in a list, tho when tapping, the full area of the list item is highlighted. Is there a way to separate the two buttons? In this case I've got an Action button and an Info button: I found this question, tho no direct solution.…
Heestand XYZ
  • 1,993
  • 3
  • 19
  • 40
47
votes
19 answers

Xcode Canvas for SwiftUI previews does not show up

I´m trying to get the new Canvas feature from Xcode 11 running, but the Canvas won´t show up. What am I doing wrong? This new Xcode feature should show a live preview of my SwiftUI views without running the app. When you create a custom View with…
stk
  • 6,311
  • 11
  • 42
  • 58
46
votes
2 answers

Why does Archive fail if there is code for SwiftUI Previews in the Preview Content folder?

In my SwiftUI app, I have put some sample model objects in Xcode's "Preview Content" folder. I only access them in my SwiftUI PreviewProviders. My understanding is that the code/assets in this folder are only used for previewing in the Canvas. My…
Jeremy
  • 2,801
  • 2
  • 30
  • 31
46
votes
2 answers

Undefined symbols ___llvm_profile_runtime

As of Xcode 11 using SwiftUI, it looks very difficult/no way to enable code coverage in unit tests and have preview working. This is what I did: Select target -> Edit scheme in Xcode Select Test tab and under Options tab, select Gather coverage for…
UndergroundFox
  • 1,011
  • 2
  • 12
  • 11
45
votes
1 answer

How to use UIWindowScene.windows on iOS 15?

Currently, with iOS 14.6, I can call a function in my app that displays a share sheet using the following code: func share(link: URL) { let activityView = UIActivityViewController(activityItems: [link], applicationActivities: nil) …
calebm
  • 485
  • 1
  • 4
  • 8
45
votes
4 answers

SwiftUI Section header - use non uppercase?

Creating a List as follows: struct ContentView: View { var body: some View { List { Section(header: Text("Header")) { Text("Row 1") Text("Row 2") } } …
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
45
votes
3 answers

How to detect keyboard events in SwiftUI on macOS?

How can I detect keyboard events in a SwiftUI view on macOS? I want to be able to use key strokes to control items on a particular screen but it's not clear how I detect keyboard events, which is usually done by overriding the keyDown(_ event:…
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76