On my macOS app, I have a main toolbar at top.
However, I would like to add a new bottom toolbar at bottom (smaller).
Like that for example:
It seems there is no way to do this on macOS as on iOS:
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
// add wine
AddButton()
Spacer()
}
}
=> .bottomBar doesn't exist on macOS.
Well, just before creating a custom view/content with some buttons, I would like to be sure there is no official way to do that on macOS (with SwiftUI only...).
Related
I am building a full screen function which is supposed to hide the navigation bar and the side bar. However, I have only found a way to toggle the sidebar using NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil), which is not working well in my case because I cannot detect if the side bar is visible or not on mac (.onDisappear does not seem to be called on mac when the sidebar collapses).
Below is a simplified example of my NavigationView. I did not use NavigationSplitView because it does not work in my case unless I use some nasty hacks (which leads to crashes too).
let data: [String] = createData()
NavigationView {
List(data, id:\.self) { item in
NavigationLink {
DetailView(item)
} label: {
Text(item)
}
}
DetailView("Nothing Selected")
}
Context
I have an app that runs only from the macOS menubar. (The LSUIElement property in info.plist is set to YES).
Instead of a menu, this app shows an NSPopover when the menubar button is clicked. The popover holds an NSHostingView which has an extremely simple SwiftUI view:
struct PopoverContentView: View
{
#State private var color: CGColor = .white
var body: some View
{
ColorPicker(selection: $color) {
Text("Pick a Color:")
}
}
}
Problem
Clicking on the ColorPicker() does not open the macOS color picker window. The UI of the ColorPicker() button changes, to show the "selected" border state but the color-picker window never appears.
However, if I change LSUIElement to be NO and then make the app active by clicking its Dock icon (so that it takes over the menubar), THEN clicking on the ColorPicker() in the popover actually reveals the color-picker window.
Do you know of a way to force macOS to show the color-picker window for a background application?
The answer turned out to be simple. In the AppKit ViewController that opens the popover when the menubar button is clicked (PopoverController, for me), I simply did this:
extension PopoverController: NSPopoverDelegate
{
func popoverWillShow(_ notification: Notification)
{
NSApp.activate(ignoringOtherApps: true)
}
}
The ColorPicker now correctly shows the standard macOS system color panel on click.
I'm trying to combine Picker and some buttons inside Menu in macOS SwiftUI app. Unfortunately Picker is folding into submenu automatically and I'm struggle to find a solution. How to prevent Picker to fold, or maybe there is a better solution around?
Menu("Budgets") {
Picker("Budgets", selection: $account) {
Button("Personal") {}.tag(1)
Button("Business") {}.tag(2)
}.labelsHidden()
Divider()
Button("New Budget…") {}
Button("Manage Budgets…") {}
}
You need inline picker style, like
Picker("Budgets", selection: $account) {
Button("Personal") {}.tag(1)
Button("Business") {}.tag(2)
}
.labelsHidden()
.pickerStyle(.inline) // << here !!
I am trying to get a customized Picker in SwiftUI for MacOS. The best option I would like to have is a customized view which can be pressed and then shows the Picker options like a ComboBox in AppKit.
Apple achieves that in their Contact books app on Mac or the Sharing dialog of Apple is the same way.
You press the + Button and the selection comes up. Is that possible in SwiftUI?
Edit: This looks very similar like contextMenu in SwiftUI. But how can I set it on left click instead?
Finally found MenuButton which does the trick for SwiftUI in MacOS.
MenuButton(label: Title(), content: {
Button(action: {
print("Clicked an item")
}) {
Text("Menu Item Text")
}
})
.menuButtonStyle(BorderlessButtonMenuButtonStyle())
... with using a custom View Title() for my clickable button.
struct Title: View {
var body: some View {
HStack
{
Text("Title")
}
}
}
I've been successful using SwiftUI for a few months with very little Xcode experience. I'm trying to add some simple help screens that I would like to preview in the SwiftUI canvas. However, I'm receiving the following error:
OptimizationLevelError: not building -Onone
The message is shown when pressing the "diagnostics" button. Problem persists after pressing "try again" button.
Xcode: Version 11.3 (11C29)
Catalina: 10.15.2 (19C57)
The code could not be simpler:
import SwiftUI
struct HelpSortView: View {
var body: some View {
VStack {
Text("You can sort the list by Name, Sail Number, Class, or Favorites.")
.lineLimit(10)
VStack(alignment: .center) {
Image("help-sort")
}
}
}
}
struct HelpSortView_Previews: PreviewProvider {
static var previews: some View {
HelpSortView()
}
}
It turns out that I had my Xcode scheme, Build Configuration set to Release instead of debug. In Xcode, select Product, Scheme, Edit Scheme, Run (on left side), Info tab, Build Configuration, set drop-down to Debug.