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
416
votes
16 answers

What is the "some" keyword in Swift(UI)?

The new SwiftUI tutorial has the following code: struct ContentView: View { var body: some View { Text("Hello World") } } The second line the word some, and on their site is highlighted as if it were a keyword. Swift 5.1 does not…
Half
  • 5,078
  • 2
  • 10
  • 20
318
votes
15 answers

SwiftUI text-alignment

Among the many properties of the Text view, I couldn't find any related to text alignment. I've seen in a demo that it automatically handles RTL, and when placing stuff using View's body, it always centers it automatically. Is there some concept…
inokey
  • 5,434
  • 4
  • 21
  • 33
282
votes
20 answers

SwiftUI - How do I change the background color of a View?

I'm beginning to try out SwiftUI and I'm surprised that it doesn't seem to be straightforward to change the background color of a View. How do you do this using SwiftUI?
jeremyabannister
  • 3,796
  • 3
  • 16
  • 25
272
votes
7 answers

SwiftUI: How to implement a custom init with @Binding variables

I am working on a money input screen and I need to implement a custom init to set a state variable based on the initialized amount. I thought the following would work: struct AmountView : View { @Binding var amount: Double @State var…
keegan3d
  • 10,357
  • 9
  • 53
  • 77
254
votes
25 answers

How to resize Image with SwiftUI?

I have a large image in Assets.xcassets. How to resize this image with SwiftUI to make it small? I tried to set frame but it doesn't work: Image(room.thumbnailImage) .frame(width: 32.0, height: 32.0)
Daniil Subbotin
  • 6,138
  • 5
  • 20
  • 24
254
votes
18 answers

Make a VStack fill the width of the screen in SwiftUI

Given this code: import SwiftUI struct ContentView: View { var body: some View { VStack(alignment: .leading) { Text("Title") .font(.title) Text("Content") .lineLimit(nil) .font(.body) Spacer() …
ielyamani
  • 17,807
  • 10
  • 55
  • 90
240
votes
14 answers

Activity indicator in SwiftUI

Trying to add a full screen activity indicator in SwiftUI. I can use .overlay(overlay: ) function in View Protocol. With this, I can make any view overlay, but I can't find the iOS default style UIActivityIndicatorView equivalent in SwiftUI. How…
Johnykutty
  • 12,091
  • 13
  • 59
  • 100
232
votes
10 answers

Round Specific Corners SwiftUI

I know you can use .cornerRadius() to round all the corners of a swiftUI view but is there a way to round only specific corners such as the top?
Richard Witherspoon
  • 4,082
  • 3
  • 17
  • 33
213
votes
26 answers

How to remove the default Navigation Bar space in SwiftUI NavigationView

I am new to SwiftUI (like most people) and trying to figure out how to remove some whitespace above a List that I embedded in a NavigationView. In this image, you can see that there is some white space above the List. What I want to accomplish is…
Vapidant
  • 2,532
  • 2
  • 12
  • 26
198
votes
8 answers

SwiftUI @State var initialization issue

I would like to initialise the value of a @State var in SwiftUI through the init() method of a Struct, so it can take the proper text from a prepared dictionary for manipulation purposes in a TextField. The source code looks like this: struct…
Daniel Messner
  • 2,305
  • 2
  • 15
  • 15
191
votes
16 answers

How do I create a multiline TextField in SwiftUI?

I've been trying to create a multiline TextField in SwiftUI, but I can't figure out how. This is the code I currently have: struct EditorTextView : View { @Binding var text: String var body: some View { TextField($text) …
gabriellanata
  • 3,946
  • 2
  • 21
  • 27
188
votes
14 answers

Get index in ForEach in SwiftUI

I have an array and I want to iterate through it initialize views based on array value, and want to perform action based on array item index When I iterate through objects ForEach(array, id: \.self) { item in CustomView(item: item) .tapAction…
Pavel
  • 3,900
  • 6
  • 32
  • 41
183
votes
35 answers

How to hide keyboard when using SwiftUI?

How to hide keyboard using SwiftUI for below cases? Case 1 I have TextField and I need to hide the keyboard when the user clicks the return button. Case 2 I have TextField and I need to hide the keyboard when the user taps outside. How I can do this…
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
175
votes
32 answers

How can I pop to the Root view using SwiftUI?

Finally now with Beta 5 we can programmatically pop to a parent View. However, there are several places in my app where a view has a "Save" button that concludes a several step process and returns to the beginning. In UIKit, I use…
Chuck H
  • 7,434
  • 4
  • 31
  • 34
174
votes
10 answers

Find all available images for Image(systemName:) in SwiftUI

Where can I find all the system images that are available in the initializer Image(systemName:)? I've only been using "chevron" and "star.fill" so far, as discovered in Apple's SwiftUI tutorial series. However, I haven't been able to find a full…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
1
2 3
99 100