Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
90 votes
7 answers
55k views

How can I change a SwiftUI Color to UIColor?

I'm trying to change a SwiftUI Color to an instance of UIColor. I can easily get the RGBA from the UIColor, but I don't know how to get the "Color" instance to return the corresponding RGB ...
Gavin Jensen's user avatar
  • 1,521
45 votes
4 answers
39k views

Define macOS window size using SwiftUI

I'm using SwiftUI to develop a new macOS application and can't figure out how to define the window size. In the AppDelegate, the window size is defined as shown below: // --- AppDelegate.swift --- ...
wigging's user avatar
  • 8,976
38 votes
3 answers
16k views

How to create Status Bar icon and menu in macOS using SwiftUI

What is SwiftUI API for creating status bar menus? Apple seems to use SwiftUI views in Battery & WiFi menus according to the accessibility inspector. Screenshot of a battery menu attached, also ...
Ruzard's user avatar
  • 1,317
37 votes
5 answers
7k views

SwiftUI – Multi column TableView in macOS

I have been experimenting a bit with SwiftUI and can’t seem to find any information at all about how to use SwiftUI to create a multi column TableView as in NSTableView. All the examples I have ...
Peter's user avatar
  • 892
32 votes
6 answers
6k views

How to programatically open Settings/Preferences window in a macOS SwiftUI App

I have created a simple macOS-only SwiftUI app and added a Settings screen as per Apple's instructions: import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { ...
Joshua's user avatar
  • 1,603
32 votes
4 answers
35k views

Displaying an empty view in SwiftUI

In SwiftUI there's frequently a need to display an "empty" view based on some condition, e.g.: struct OptionalText: View { let text: String? var body: some View { guard let text = text else {...
Max Desiatov's user avatar
  • 5,426
32 votes
7 answers
37k views

How to access own window within SwiftUI view?

The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or ...
Asperi's user avatar
  • 250k
30 votes
5 answers
30k views

SwiftUI - How to add foreground linear gradient on image

I am unable to find any related documentation on how to do a linear gradient on the foreground for an image I have with SwiftUI. I have tried to do it like so: Image("IconLoseWeight") .frame(width:...
Rodrigo Mata's user avatar
  • 1,849
30 votes
7 answers
13k views

How do I debug SwiftUI AttributeGraph cycle warnings?

I'm getting a lot of AttributeGraph cycle warnings in my app that uses SwiftUI. Is there any way to debug what's causing it? This is what shows up in the console: === AttributeGraph: cycle detected ...
Sindre Sorhus's user avatar
30 votes
6 answers
37k views

How can I make a simple circular button in SwiftUI in macOS?

Seems like it should be simple. Button(action: { }){ ZStack { Circle() .frame(width: 100, height: 100) .foregroundColor(.blue) Text("Press me") ...
ShadyAmoeba's user avatar
29 votes
5 answers
12k views

macOS SwiftUI Navigation for a Single View

I'm attempting to create a settings view for my macOS SwiftUI status bar app. My implementation so far has been using a NavigationView, and NavigationLink, but this solution produces a half view as ...
Husker28's user avatar
  • 393
29 votes
2 answers
6k views

SwiftUI macOS fetch json error [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_* flags\

I tired to build an macOS application (Xcode 12, SwifUI) for my IOS app. To fetch json data from my website I need a fetch request (no API key). I found several samples on medium, hackingwithswift etc....
DoTryCatch's user avatar
  • 1,062
28 votes
2 answers
16k views

How to open a FileDialog?

In a SwiftUI app on macOS I want to allow a user to select a file from the macOS filesystem. I tried to use AppKit's NSOpenPanel. I tried like this, but I'm unable to create the ...
mica's user avatar
  • 4,230
28 votes
7 answers
11k views

How to open another window in SwiftUI, macOS?

I'd like to show a second window with different content in a SwiftUI app on macOS. I can't find any documentation on this. The attempt below doesn't work. Does anyone know how to do it? class AppState:...
Rob N's user avatar
  • 15.9k
27 votes
7 answers
14k views

Issue with Buttons in SwiftUI on MacOS

I'm using SwiftUI on MACOS If I do this: Button(action: { } ) { Text("Press") .padding() .background(Color.blue) } I get this: and the two grey areas are the ...
Cortado-J's user avatar
  • 2,217
27 votes
5 answers
21k views

How do I add a toolbar to a macOS app using SwiftUI?

I am trying to add a toolbar inside the title bar to a macOS app using SwiftUI, something similar to what is shown below. I am unable to figure out a way to achieve this using SwiftUI. Currently, I ...
Bijoy Thangaraj's user avatar
26 votes
2 answers
3k views

SwiftUI macOS right sidebar inspector

I have a document-based SwiftUI app. I'd like to make a inspector sidebar like the one in Xcode. Starting with Xcode's Document App template, I tried the following: struct ContentView: View { @...
Taylor's user avatar
  • 6,193
25 votes
4 answers
39k views

In SwiftUI, How do I increase the height of a button?

As you can see in the screenshot, the button height does not adjust to fit the text size, making it look ugly. How can I increase the hight of the buttons, so it does not look stupid. My question is, ...
user avatar
25 votes
0 answers
2k views

SwiftUI and Catalyst: [API] cannot add handler to 3 from 3 - dropping

After converting my SwiftUI project to Catalyst I get this strange error message on the console: [API] cannot add handler to 3 from 3 - dropping This happens when a view is scrolled, even in release ...
heiko's user avatar
  • 1,356
24 votes
4 answers
9k views

SwiftUI on macOS: How to enable UI for onDelete (deletion from List)

Perhaps I'm particularly dense this morning but I'm trying to delete a row from a List in SwiftUI on macOS. The issue is that there is no UI exposed to perform the delete. By that I mean that the ...
sas's user avatar
  • 7,392
23 votes
7 answers
12k views

Is there a way to use SF Symbols in macOS SwiftUI development?

SF Symbols provides a set of over 2,400 consistent, highly configurable symbols you can use in your app. Apple designed SF Symbols to integrate seamlessly with the San Francisco system font, so the ...
fankibiber's user avatar
23 votes
7 answers
28k views

Cannot find TestView in scope

I am seeing the following error in iOS14, I am not sure if it is a bug or a user error :-) I appreciate it is still in beta, so I will post this in the developer forums as well. I am seeing the ...
jwknz's user avatar
  • 6,734
22 votes
4 answers
7k views

SwiftUI on Mac - How do I designate a button as being the primary?

In AppKit I would do this by assigning its key equivalent to be ↩ or making its cell the window's default. However, neither of these seems possible in SwiftUI, so how do I make a button the default ...
Ky -'s user avatar
  • 31.7k
22 votes
3 answers
11k views

Scroll SwiftUI List to new selection

If you have a SwiftUI List with that allows single selection, you can change the selection by clicking the list (presumably this makes it the key responder) and then using the arrow keys. If that ...
Brandon Horst's user avatar
20 votes
5 answers
6k views

Is there a way not to show a window for a SwiftUI-lifecycle macOS app?

I'm looking to not show a window for a SwiftUI application on macOS. The app uses SwiftUI's application lifecycle and only runs in the status bar. Showing a window on start up is unnecessary. I'm ...
Kilian's user avatar
  • 2,252
20 votes
2 answers
15k views

Why can't one iterate through enum data within a SwiftUI view?

Say I have some enum, Channel, filled with sample property data: enum Channel: CaseIterable { case abc case efg case hij var name: String { switch self { case .abc: return "...
JDev's user avatar
  • 5,478
20 votes
1 answer
5k views

Why does URL Scheme/onOpenURL in SwiftUI always open a new window?

I'm converting an old macOS app to SwiftUI, and I've run into a problem with the new SwiftUI WindowGroup. The old app is a single window application (basically a glorified timer) and an URL scheme (...
Simon's user avatar
  • 515
20 votes
1 answer
3k views

SwiftUI Nested Scrollviews problem on macOS

(If any Apple folks see this - I filed a feedback: FB8910881) I am trying to build a an SwiftUI app with a layout similar to the Apple Music app - Artist Page or like in the SwiftUI tutorial from ...
Denise's user avatar
  • 360
20 votes
4 answers
4k views

List or OutlineGroup expanded by default in SwiftUI

With List or OutlineGroup in SwiftUI how to make some (or all) of their branches expanded by default when creating the view. This seems to be possible with DisclosureGroup with a binding. This could ...
João Colaço's user avatar
19 votes
2 answers
7k views

SwiftUI: Remove 'Focus Ring' Highlight Border from macOS TextField

I used the below code to create a custom search bar in SwiftUI. It works great on iOS / Catalyst: ...but when running natively on macOS, the 'focus ring' highlighted border styling (when the user ...
TheNeil's user avatar
  • 3,691
18 votes
2 answers
5k views

Struct 'State' cannot be used as an attribute

I have the following code in a ContentView.swift file: struct ContentView: View { @State private var selectedSpeed: Int = 1 var body: some View { Text("Hello World") ...
jjramos's user avatar
  • 2,064
18 votes
9 answers
4k views

SwiftUI: The Structured Editing Popover (Command + Click) does not show up

In SwiftUI tutorial (link) it says to: Command-click the text view’s initializer to show the structured editing popover, and then choose Embed in VStack However, it does not work for me. What is ...
zeh's user avatar
  • 1,307
18 votes
5 answers
7k views

Rotate a Text view and its frame in SwiftUI

I can rotate the Text in SwiftUI using rotationEffect but it doesn't rotate the frame. As shown in the image, the text is rotated but the frame is still horizontal. I would like to rotate the frame ...
wigging's user avatar
  • 8,976
17 votes
4 answers
16k views

SwiftUI Drag and Drop files

I am trying to add a "Drag and Drop" gesture / function to my SwiftUI Mac application. I want to drop files from my System/ Desktop into my Application. It is possbile in regular Swift, which I ...
davidev's user avatar
  • 8,301
17 votes
1 answer
7k views

SwiftUI: Get the Dynamic Background Color (Dark Mode or Light Mode)

Is there a way to systematically access the standard, dynamic background color for SwiftUI views, regardless of whether the user be in Light or Dark Mode? For example, I know the following can be ...
TheNeil's user avatar
  • 3,691
17 votes
4 answers
4k views

NSWindow contentView not cover full window size - macOS & SwiftUI

I'm starting a new macOS app with SwiftUI but I have a big problem. The app needs a full size contentView (underneath titleBar) but I can't accomplish. On a new project using Storyboards works fine, ...
mhergon's user avatar
  • 1,658
16 votes
5 answers
5k views

How do I disable the Show Tab Bar menu option in SwiftUI

I have created a very simple app on MacOS created with SwiftUI. Default there is a menu item show tab bar. How do I remove this? I doesn't make sense to have tabs in this app. I've found the following ...
ngstschr's user avatar
  • 2,239
16 votes
2 answers
15k views

Can you use a Publisher directly as an @ObjectBinding property in SwiftUI?

In SwiftUI, can you use an instance of a Publisher directly as an @ObjectBinding property or do you have to wrap it in a class that implements BindableObject? let subject = PassthroughSubject<...
kennyc's user avatar
  • 5,630
16 votes
2 answers
11k views

How do I set a minimum size for a window and make it use the specified size and position when launching?

I am making a MacOS app with the default SwiftUI Application template. How can I set the minimum size of the window to (800, 500)? Also if I close the window, and reopen, it reopens as the size and ...
user avatar
16 votes
5 answers
7k views

SwiftUI 2: the way to open view in new window

Lets imagine that I have an App var storeVM = BookStoreViewModel(bla1: bla1, bla2: bla2, bla3: bla3) @SceneBuilder var body: some Scene { WindowGroup { BookStoreView( model: storeVM ) ...
Andrew_STOP_RU_WAR_IN_UA's user avatar
16 votes
1 answer
7k views

Understanding Scene/WindowGroup in SwiftUI 2?

There is several objects in SwiftUI 2 like Scene and WindowGroup as some sort of Scene. After reading documentation and looking WWDC2020 video related to scenes I see hierarchy by the following way : ...
Andrew_STOP_RU_WAR_IN_UA's user avatar
16 votes
1 answer
3k views

Setting the cursor position within a SwiftUI TextEditor

Is there any way to programmatically move the cursor to a specific text line or select it within a SwifUI TextEditor? For example, if there is a TextEditor with 10 lines written in it. When the user ...
Battle Eagle's user avatar
16 votes
2 answers
2k views

How to make SwiftUI's Picker borderless/transparent on macOS?

I want to set a picker button borderless and transparent in SwiftUI on macOS. I tried using .background(), .border() and .opacity() modifiers on the Picker view. However, I can't change the ...
Wowbagger and his liquid lunch's user avatar
15 votes
3 answers
9k views

Setting a main window title for macOS SwiftUI app in AppKit?

I have a simplistic WKWebView app that opens up a website on macOS, using SwiftUI in AppKit. However, the app window has no title - I'm talking of the top row (with the red X to close it, etc. How do ...
esaruoho's user avatar
  • 946
15 votes
3 answers
12k views

SwiftUI - If inside ForEach loop

I am building a List based on my elements in an array I fetched before. I am fetching all the entities.. when the user makes a search in the search bar, I want to filter my List. I am NOT doing a new ...
lvollmer's user avatar
  • 1,568
15 votes
3 answers
4k views

'Failed to finalize LSBundleWrapper' on SwiftUI app build for release

I have a new app which I'm building to be cross platform using SwiftUI When I build for release I get a very odd error in a popup generated by Xcode. Unable To Install ‚Äúnomadgo.ProductivityDashboard‚...
Jon Vogel's user avatar
  • 5,506
15 votes
3 answers
7k views

How to access NSWindow from @main App using only SwiftUI?

At this answer the solution work for Scene plus swiftUI. However using @main like: @main struct MyApp: App { @StateObject private var model = MyModel() var body: some Scene { ...
Amadeu Cavalcante Filho's user avatar
15 votes
5 answers
10k views

Close Window and open another one

I'm new to SwiftUI and it's so much to overthink after using storboards for years. I try to convert my previous storyboard application and unfortunately, there are only tutorials for iOS. Nevermind, ...
Lupurus's user avatar
  • 4,039
15 votes
3 answers
13k views

SwiftUI: how to use NavigationView in macOS?

I have two separate Xcode projects that I'm using to learn SwiftUI: A true macOS project (not Catalyst) on Mac. An iOS project (iPhone). The following code creates a simple NavigationView with ...
Salavat Khanov's user avatar
15 votes
2 answers
4k views

UIDocumentPickerViewController in SwiftUI on mac (macCatalyst)

So I've been meddling with "moving" a small SwiftUI iPad app to the Mac, and I've hit a bit of a speed bump with UIDocumentPickerViewController. I have wrapped the UIDocumentPickerViewController in a ...
ALex Popa's user avatar
  • 339

1
2 3 4 5
35