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
77
votes
8 answers

How to display a search bar with SwiftUI

The new SwiftUI framework does not seem to provide a built-in search bar component. Should I use a UISearchController and wrap it in some way, or should I use a simple textfield and update the data according to the textfield input? 2019 EDIT: A…
Antoine Weber
  • 1,741
  • 1
  • 14
  • 14
76
votes
15 answers

How to add a TextField to Alert in SwiftUI?

Anyone an idea how to create an Alert in SwiftUI that contains a TextField?
Captain Vril
  • 781
  • 1
  • 5
  • 6
76
votes
12 answers

Set Toggle color in SwiftUI

I've implemented a toggle after following Apple's tutorial on user input. Currently, it looks like this: This is the code that produces this UI: NavigationView { List { Toggle(isOn: $showFavoritesOnly) { Text("Show Favorites…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
74
votes
17 answers

Isn't there an easy way to pinch to zoom in an image in SwiftUI?

I want to be able to resize and move an image in SwiftUI (like if it were a map) with pinch to zoom and drag it around. With UIKit I embedded the image into a UIScrollView and it took care of it, but I don't know how to do it in SwiftUI. I tried…
Zheoni
  • 881
  • 1
  • 7
  • 11
73
votes
11 answers

Xcode 11 beta swift ui preview not showing

Just playing with Swift UI basic app and the preview canvas is not showing even though I'm in canvas mode. App runs, and I have this little snippet what am I missing? #if DEBUG struct ContentView_Previews : PreviewProvider { static var previews:…
MadeByDouglas
  • 2,509
  • 1
  • 18
  • 22
73
votes
4 answers

Equal widths of subviews with SwiftUI

I'm trying to build a simple watchOS UI with SwiftUI with two pieces of information side-by-side above a button. I'd like each side (represented as a VStack within an HStack) to take up half of the available width (so it's an even 50/50 split within…
gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
73
votes
15 answers

Showing 'UIActivityViewController' in SwiftUI

I want to let the user to be able to share a location but I don't know how to show UIActivityViewController in SwiftUI.
nOk
  • 2,725
  • 4
  • 13
  • 26
72
votes
9 answers

SwiftUI: Automatic preview updating paused, always

I have an existing App, basically a shopping list app, to which I'm trying to add some sweet sweet SwiftUI lovin. My issue is the real time preview updating doesn't work - the warning "Automatic preview updating paused" continually shows. I hit the…
Brian M
  • 3,812
  • 2
  • 11
  • 31
72
votes
5 answers

How to get the iPhone's screen width in SwiftUI?

I want to resize an Image frame to be a square that takes the same width of the iPhone's screen and consequently the same value (screen width) for height. The following code don't work cause it gives the image the same height of the view. var…
Rubens Neto
  • 802
  • 1
  • 6
  • 8
72
votes
6 answers

How to access safe area size in SwiftUI?

I want to manually set the frame height of a view in SwiftUI to the size of the safe area of the screen. It's easy to get the bounds of the screen (UIScreen.main.bounds), but I can't find a way to access the size of the safe area.
M. Koot
  • 1,046
  • 1
  • 9
  • 11
71
votes
5 answers

How can I set an image tint in SwiftUI?

I am trying to set an image tint in SwiftUI Image class For UIKit, I can set image tint using let image = UIImage(systemName: "cart")!.withTintColor(.blue) but I cant find such settings in swiftui docs
Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31
71
votes
4 answers

How do I allow text selection on a Text label in SwiftUI?

When I create a text view: Text("Hello World") I can't allow the user to select text when they long press. I've looked at using a TextField but that doesn't seem to allow for turning off text editing. I just want to be able to display a body of…
mralexhay
  • 1,164
  • 1
  • 10
  • 16
71
votes
4 answers

How can I hide/remove ScrollBar in ScrollView in SwiftUI?

If the content of the ScrollView is bigger than the screen, while scrolling, the scrollbar on the side appears. I couldn't find anything to help me hide it.
Hamoonist
  • 2,286
  • 3
  • 19
  • 36
71
votes
6 answers

Show line / separator view in SwiftUI

I want to show a separator line in my SwiftUI app. To achieve that, I tried to create an empty view with a fixed frame and a background color / border: EmptyView() .frame(width: 200, height: 2) .background(Color.black) // or: …
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
69
votes
6 answers

How to define a protocol as a type for a @ObservedObject property?

I have a swiftui view that depends on a view model, the view model has some published properties. I want define a protocol and default implementation for the view model hierarchy, and make the view dependent on the protocol not the concrete class? I…
M.Serag
  • 1,381
  • 1
  • 11
  • 15