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
53
votes
9 answers

Center View horizontally in SwiftUI

How can I center horizontally a View (Image) in an HStack? I want a button to be left aligned and the image to be centered horizontally the view. Currently I have this structure: VStack { HStack { Button(action: { …
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
52
votes
11 answers

SwiftUI 2.0 CoreData issues with new project - 'Cannot find type 'Item' in scope'

[EDIT] I was hoping that Apple would have fixed what is obviously a bug. The solution is to clear the cache, close and reopen Xcode.. I am on Xcode Beta and starting a new project and without writing a single line of code I already get an error. I…
multitudes
  • 2,898
  • 2
  • 22
  • 29
52
votes
6 answers

SwiftUI - Add Border to One Edge of an Image

It's a pretty straight-forward question - How does one apply a border effect to only the wanted edges of an Image with SwiftUI? For example, I only want to apply a border to the top and bottom edges of an image because the image is taking up the…
Justin Frazer
  • 740
  • 1
  • 5
  • 19
52
votes
9 answers

Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`?

EDIT: This has been fixed in iOS 13.3! Minimal reproducible example (Xcode 11.2 beta, this works in Xcode 11.1): struct Parent: View { var body: some View { NavigationView { Text("Hello World") …
Robert
  • 981
  • 1
  • 15
  • 24
52
votes
6 answers

SwiftUI automatically scroll to bottom in ScrollView (Bottom first)

My problem is that i have (in SwiftUI) a ScrollView with an foreach inside. Know when the foreach loads all of my entries i want that the last entry is focused. I did some google research, but i didn't find any answer. ScrollView { VStack { …
Leahpar
  • 583
  • 1
  • 4
  • 12
52
votes
7 answers

SwiftUI withAnimation completion callback

I have a swiftUI animation based on some state: withAnimation(.linear(duration: 0.1)) { self.someState = newState } Is there any callback which is triggered when the above animation completes? If there are any suggestions on how to accomplish…
Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
52
votes
3 answers

In SwiftUI, how to use UIHostingController inside an UIView or as an UIView?

An alternative question title could be "How to add an UIHostingController's view as subview for an UIView?". I am creating a new piece of UI component and would love to give SwiftUI a try. The image below is the current view structure. The UIView is…
XY L
  • 25,431
  • 14
  • 84
  • 143
51
votes
8 answers

SwiftUI – @State vs @Binding

I am learning iOS Programming with Swift and SwiftUI. I know very little and I am very confused about the difference between a @State and a @Binding. If I understood it correctly, @Binding is just technically @State but it doesn't update the view.…
Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107
51
votes
1 answer

SwiftUI Row Height of List - how to control?

I have a simple List in SwiftUI. Code and Screenshot included below. I would like to reduce the height of each row in the list (so less space between lines and text lines closer together). I already tried to add a ".frame(height: 20)" to the HStack…
Gerard
  • 1,807
  • 4
  • 13
  • 20
51
votes
4 answers

Get width of a view using in SwiftUI

I need to get width of a rendered view in SwiftUI, which is apparently not that easy. The way I see it is that I need a function that returns a view's dimensions, simple as that. var body: some View { VStack(alignment: .leading) { …
Alexey Primechaev
  • 907
  • 1
  • 6
  • 13
51
votes
7 answers

How to set custom highlighted state of SwiftUI Button

I have a Button. I want to set custom background color for highlighted state. How can I do it in SwiftUI? Button(action: signIn) { Text("Sign…
Daniil Subbotin
  • 6,138
  • 5
  • 20
  • 24
50
votes
1 answer

SwiftUI configure LazyVGrid with no spacing

I wanted to create a grid of cells with no spaces or smaller space just like the Photos app, is it possible with SwiftUI 2 LazyVGrid? I've tried it but there is always this space in-between columns. In the documentation, the spacing parameter is…
randomor
  • 5,329
  • 4
  • 46
  • 68
50
votes
5 answers

Is it possible to change "return" key to "done" on keyboard with SwiftUI?

I didn't find any link or guide to change the "return" key to "done" when keyboard open for TextField in SwiftUI. Is it possible now without customising UITextField?
Andro Developer
  • 1,323
  • 3
  • 12
  • 22
50
votes
8 answers

SwiftUI - Navigation bar button not clickable after sheet has been presented

I have just started using SwiftUI a couple of weeks ago and i'm still learning. Today I ran into an issue. When I present a sheet with a navigationBarItems-button and then dismiss the ModalView and return to the ContentView I find myself unable to…
Markus Moltke
  • 970
  • 1
  • 8
  • 20
50
votes
3 answers

How can I avoid nested Navigation Bars in SwiftUI?

Using SwiftUI, I've built a NavigationView that takes the user to another NavigationView, and finally, to a simple View. When I get to the last view, I can see two back buttons and a very large Navigation Bar. I'd like to have a navigation…
Thiago Mundim
  • 523
  • 1
  • 4
  • 7