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
136
votes
7 answers

SwiftUI - How to pass EnvironmentObject into View Model?

I'm looking to create an EnvironmentObject that can be accessed by the View Model (not just the view). The Environment object tracks the application session data, e.g. loggedIn, access token etc, this data will be passed into the view models (or…
Michael
  • 1,769
  • 2
  • 12
  • 21
130
votes
28 answers

SwiftUI update navigation bar title color

How to change the navigation bar title color in SwiftUI NavigationView { List { ForEach(0..<15) { item in HStack { Text("Apple") .font(.headline) .fontWeight(.medium) …
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
127
votes
29 answers

How do I modify the background color of a List in SwiftUI?

I'm trying to recreate an UI I built with UIKit in SwiftUI but I'm running into some minor issues. I want the change the color of the List here, but no property seems to work as I expects. Sample code below: struct ListView: View { …
andromedainiative
  • 4,414
  • 6
  • 22
  • 34
126
votes
14 answers

How to print() to Xcode console in SwiftUI?

So I tried to put a print statement while debugging in a SwiftUI View. print("landmark: \(landmark)") In the following body. var body: some View { NavigationView { List { Toggle(isOn: $userData.showFavoritesOnly) { …
zumzum
  • 17,984
  • 26
  • 111
  • 172
126
votes
7 answers

Custom font size for Text in SwiftUI

I have a label in my view that I want to use the system font size in medium, with a size of 21 points. I created a custom extension to re-use the font created: extension Font { static var primaryButton: Font { return…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
125
votes
13 answers

SwiftUI Button tap only on text portion

The background area of my button is not detecting user interaction. Only way to interact with said button is to tap on the Text/ Label area of the button. How to make entire Button tappable? struct ScheduleEditorButtonSwiftUIView: View { …
RyanTCB
  • 7,400
  • 5
  • 42
  • 62
123
votes
5 answers

How do I set the size of a SF Symbol in SwiftUI?

How do I set the size of a SF Symbol in Xcode 11 using SwiftUI?
Ryan
  • 10,798
  • 11
  • 46
  • 60
122
votes
13 answers

SwiftUI can't tap in Spacer of HStack

I've got a List view and each row of the list contains an HStack with some text view('s) and an image, like so: HStack{ Text(group.name) Spacer() if (groupModel.required) { Text("Required").color(Color.gray) } …
Quinn
  • 7,072
  • 7
  • 39
  • 61
121
votes
9 answers

SwiftUI. How to change the placeholder color of the TextField?

I want to change the placeholder color of the TextField, but I can't find a method for it. I tried to set foregroundColor and accentColor, but it doesn't change the placeholder color. Here is the code: TextField("Placeholder", $text) …
Ilya Kharabet
  • 4,203
  • 3
  • 15
  • 29
120
votes
13 answers

Use Hex color in SwiftUI

In UIKit we could use an Extension to set hex color to almost everything, like in this tutorial. But when I'm trying to do it in SwiftUI, it's not possible, it looks like the SwiftUI does not get the UIColor as parameter. Text(text) …
SinaMN75
  • 6,742
  • 5
  • 28
  • 56
119
votes
5 answers

Why doesn't the navigation title show up using SwiftUI?

I am learning SwiftUI using Apple's official tutorial: https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation Everything works perfectly until I try to show a navigation title on a NavigationView by calling…
Yiming Dong
  • 1,530
  • 2
  • 9
  • 11
118
votes
4 answers

SwiftUI - Multiple Buttons in a List row

Say I have a List and two buttons in one row, how can I distinguish which button is tapped without the entire row highlighting? For this sample code, when any one of the buttons in the row is tapped, both button's action callbacks are invoked. // a…
Bradley Mackey
  • 6,777
  • 5
  • 31
  • 45
115
votes
10 answers

The text doesn't get wrapped in swift UI

Even After setting the .lineLimit(nil) the text doesn't get wrapped. var body: some View { VStack(alignment: .center) { Text("SwiftUI is a modern way to declare user interfaces for any Apple platform. ") .font(.title) …
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
111
votes
7 answers

SwiftUI HStack fill whole width with equal spacing

I have an HStack: struct BottomList: View { var body: some View { HStack() { ForEach(navData) { item in NavItem(image: item.icon, title: item.title) } } } } How do I perfectly center…
Tom
  • 3,672
  • 6
  • 20
  • 52
111
votes
8 answers

How do I set a weight to SF Symbols for iOS 13?

I have this Image(systemName: "arrow.right") But how do I make it bold, semibold etc? I am using the new SwiftUI.
Just a coder
  • 15,480
  • 16
  • 85
  • 138