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
84
votes
20 answers

How can I trigger an action when a swiftUI toggle() is toggled?

In my SwiftUI view I have to trigger an action when a Toggle() changes its state. The toggle itself only takes a Binding. I therefore tried to trigger the action in the didSet of the @State variable. But the didSet never gets called. Is there any…
Brezentrager
  • 879
  • 1
  • 7
  • 9
83
votes
8 answers

How can I run an action when a state changes?

enum SectionType: String, CaseIterable { case top = "Top" case best = "Best" } struct ContentView : View { @State private var selection: Int = 0 var body: some View { SegmentedControl(selection: $selection) { …
user7885981
83
votes
19 answers

SwiftUI NavigationButton without the disclosure indicator?

When making a List with a row that pushes to a new view, SwiftUI adds a disclosure indicator ">" automatically? How do I remove it if I don't want it? NavigationView { List { NavigationButton(destination: DetailView()) { …
Radde Mojsovski
  • 849
  • 1
  • 6
  • 6
80
votes
10 answers

How to show NavigationLink as a button in SwiftUI

I've read a lot here about navigation in SwiftUI and tried a couple of things, but nothing is working as desired. Basically, I have a view with a list of workouts and you can show a single workout by clicking on a row. This works as expected using…
G. Marc
  • 4,987
  • 4
  • 32
  • 49
80
votes
18 answers

SwiftUI TextField max length

Is it possible to set a maximum length for TextField? I was thinking of handling it using onEditingChanged event but it is only called when the user begins/finishes editing and not called while user is typing. I've also read the docs but haven't…
M Reza
  • 18,350
  • 14
  • 66
  • 71
79
votes
11 answers

Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

So I have a ScrollView holding a set of views: ScrollView { ForEach(cities) { city in NavigationLink(destination: ...) { CityRow(city: city) } .buttonStyle(BackgroundButtonStyle()) …
zh.
  • 1,533
  • 1
  • 14
  • 18
79
votes
1 answer

Proportional height (or width) in SwiftUI

I started exploring SwiftUI and I can't find a way to get a simple thing: I'd like a View to have proportional height (basically a percentage of its parent's height). Let's say I have 3 views vertically stacked. I want: The first to be 43% (of its…
superpuccio
  • 11,674
  • 8
  • 65
  • 93
78
votes
13 answers

Change background color of TextEditor in SwiftUI

TextEditor seems to have a default white background. So the following is not working and it displayed as white instead of defined red: var body: some View { TextEditor(text: .constant("Placeholder")) .background(Color.red) } Is it…
Lorenzo Fiamingo
  • 3,251
  • 2
  • 17
  • 35
78
votes
4 answers

How can I use edgesIgnoringSafeArea in SwiftUI, but make a child view respect the safe area?

I'm building a UI is SwiftUI with a list view, and I want to put a panel over part of the list view with a button on it. On devices with a safe area at the bottom (that don't have a physical home button) I want the panel to go to the bottom of the…
gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
78
votes
2 answers

SwiftUI: unwanted split view on iPad

Problem: a view on Pad shows up with unwanted split view. My current setup is: Catalina OSX beta 5 + Xcode 11 Beta 5 Here is the code I used, with a Navigation View and a Navigation Title: import SwiftUI struct SwiftUIView: View { var body:…
Mane Manero
  • 3,086
  • 5
  • 25
  • 47
78
votes
14 answers

Custom back button for NavigationView's navigation bar in SwiftUI

I want to add a custom navigation button that will look somewhat like this: Now, I've written a custom BackButton view for this. When applying that view as leading navigation bar item, by doing: .navigationBarItems(leading: BackButton()) ...the…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
78
votes
3 answers

What is the difference between List and ForEach in SwiftUI?

I know SwiftUI does not support currently regular for loops but instead provide something called ForEach but what is the difference between that and a List?
Chéyo
  • 9,107
  • 9
  • 27
  • 44
77
votes
7 answers

How can I change a SwiftUI Color to UIColor?

I'm trying to change a SwiftUI Color to an instance of UIColor. I can easily get the RGBA from the UIColor, but I don't know how to get the "Color" instance to return the corresponding RGB and opacity values. @EnvironmentObject var colorStore:…
Gavin Jensen
  • 1,371
  • 2
  • 9
  • 14
77
votes
12 answers

SwiftUI: How to draw filled and stroked shape?

In UIKit drawing a stroked and filled path/shape is pretty easy. Eg, the code below draws a red circle that is stroked in blue. override func draw(_ rect: CGRect) { guard let ctx = UIGraphicsGetCurrentContext() else { return } let…
orj
  • 13,234
  • 14
  • 63
  • 73
77
votes
13 answers

SwiftUI: Send email

In a normal UIViewController in Swift, I use this code to send a mail. let mailComposeViewController = configuredMailComposeViewController() mailComposeViewController.navigationItem.leftBarButtonItem?.style =…
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120