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
59
votes
13 answers

SwiftUI: how to handle BOTH tap & long press of button?

I have a button in SwiftUI and I would like to be able to have a different action for "tap button" (normal click/tap) and "long press". Is that possible in SwiftUI? Here is the simple code for the button I have now (handles only the "normal"…
Gerard
  • 1,807
  • 4
  • 13
  • 20
59
votes
6 answers

Push View programmatically in callback, SwiftUI

It seems to me that Apple is encouraging us to give up using UIViewController in SwiftUI, but without using view controllers, I feel a little bit powerless. What I would like is to be able to implement some sort of ViewModel which will emit events…
Bohdan Savych
  • 3,310
  • 4
  • 28
  • 47
59
votes
1 answer

How to make TextField align right (trailing)

I am trying to have the value of a TextField displayed with trailing alignment. As you can see the value 34.3 is display with leading alignment. I am sure I am missing something obvious but I can't figure out what. Any ideas? @State private var…
mimo
  • 741
  • 1
  • 6
  • 13
59
votes
6 answers

How to make width of view equal to superview in SwiftUI

I have Button struct ContentView : View { var body: some View { HStack { Button(action: {}) { Text("MyButton") .color(.white) .font(Font.system(size: 17)) …
Argas
  • 1,427
  • 1
  • 10
  • 12
58
votes
7 answers

SwiftUI: Global Overlay That Can Be Triggered From Any View

I'm quite new to the SwiftUI framework and I haven't wrapped my head around all of it yet so please bear with me. Is there a way to trigger an "overlay view" from inside "another view" when its binding changes? See illustration below: I figure this…
realph
  • 4,481
  • 13
  • 49
  • 104
58
votes
7 answers

How to make view the size of another view in SwiftUI

I'm trying to recreate a portion of the Twitter iOS app to learn SwiftUI and am wondering how to dynamically change the width of one view to be the width of another view. In my case, to have the underline be the same width as the Text view. I have…
Zach Fuller
  • 1,219
  • 2
  • 14
  • 18
58
votes
9 answers

Adding unlimited lines in a Text (SwiftUI)

Just figuring out how I can achieve multiple lines of text in a Text. It seems like the Text has the same default as UILabel (one line), but I can't find any function which meets this criteria. struct ContentView : View { var body: some View { …
Jacob Ahlberg
  • 2,352
  • 6
  • 22
  • 44
57
votes
15 answers

Change background color when dark mode turns on in SwiftUI

I've created a custom sheet in SwiftUI with the background color White .background(Color.white) Now I want the background color to change to black when the user turns on the dark mode on iOS. But I can't find a dynamic color for background like…
umayanga
  • 2,254
  • 3
  • 15
  • 26
57
votes
3 answers

How could I initialize the @State variable in the init function in SwiftUI?

Let's see the simple source code: import SwiftUI struct MyView: View { @State var mapState: Int init(inputMapState: Int) { mapState = inputMapState //Error: 'self' used before all stored properties are initialized }…
norains
  • 743
  • 1
  • 5
  • 12
57
votes
7 answers

Text inside a VStack truncates when it's not supposed to in SwiftUI

I'm trying to create a simple stack of Text inside a VStack, and no matter what I do, the text will truncate instead of wrap, even if I explicitly set lineLimit(nil) (although I know this is the default now). I've tried setting layoutPriority(1) on…
Adam Singer
  • 2,377
  • 3
  • 18
  • 18
57
votes
5 answers

How to change selected segment color in SwiftUI Segmented Picker

I want to set the selected segment color in a SwiftUI segmented picker and change the text color to white. I have tried both using the modifiers for the picker view and modifying the tint color from the appearance proxy. None of them seem to work,…
swifty
  • 971
  • 1
  • 8
  • 15
57
votes
5 answers

Difference between creating ViewModifier and View extension in SwiftUI

I'm trying to find out what is practical difference between these two approaches. For example: struct PrimaryLabel: ViewModifier { func body(content: Content) -> some View { content .padding() …
Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58
56
votes
3 answers

EnvironmentObject vs Singleton in SwiftUI?

If the entire views access the same model in an app, I think the Singleton pattern is enough. Am I right? For example, if MainView and ChildView access the same model(e.g. AppSetting) like below, I cannot find any reason to use EnvironmentObject…
user2848557
  • 823
  • 1
  • 9
  • 11
56
votes
5 answers

iOS Swift Combine: cancel a Set

If I have stored a cancellable set into a ViewController: private var bag = Set() Which contains multiple subscription. 1 - Should I cancel subscription in deinit? or it does the job automatically? 2 - If so, how can I cancel all…
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
56
votes
22 answers

SwiftUI hide TabBar in subview

I am working with SwiftUI, and I have some issues with the TabBar. I want to hide the TabBar on a specific subview. Have tried with UITabBar.appearance().isHidden = true It only works on the direct views in the TabView. But when I place it in a…
Dyngberg
  • 663
  • 1
  • 7
  • 10