4

I'm working on a macOS app, with the view layers written in SwiftUI. I know that iOS toolbars can have the background color changed at least, but when I try to do this in macOS, it doesn't behave as I'd expect.

Here's a (simplified) example:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Collections()
                .layoutPriority(0)
            
            Photos()
                    .frame(maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)
                    .background(Color.Alt.black)
                    .layoutPriority(1)
        }
        .toolbar {
            Toolbar().background(Color.red500)
        }
    }
}

struct Toolbar: View {
    var body: some View {
        Group {
            Slider(value: 250, in: 150...400) {
                Text("Toolbar.PreviewSize")
            } minimumValueLabel: {
                Image(systemName: "photo").resizable().scaledToFit().frame(width: 15)
            } maximumValueLabel: {
                Image(systemName: "photo").resizable().scaledToFit().frame(width: 23)
            } onEditingChanged: { _ in
                // do nothing
            }.frame(minWidth: 200)
                
            Spacer()
                    
            Text("Toolbar.SelectionCount")
                    
            Spacer()
                    
            AddPhotosButton()
        }
    }
}

Which produces something like this, which as you can see, doesn't apply the background color to the entire toolbar, just to the items in the toolbar:

Toolbar with red background on the toolbar items, but not the entire toolbar

I'm guessing I could make my own WindowToolbarStyle style, but there's no documentation on the protocol!

If I make my own toolbar as a View rather than with the .toolbar modifier, I can't read the safe area insets for the window traffic buttons when the sidebar is collapsed, resulting in a complete mess:

custom toolbar that works well when the sidebar is open custom toolbar that doesn't work when the sidebar is closed

Thanks for any help!

4
  • you could try replacing Group with HStack, then add things like padding etc... Commented Nov 29, 2021 at 22:59
  • @workingdog that might work, but that feels a bit difficult to get the padding correct, and would be subject to any changes apple made in the future. I'd prefer an official path forward. Commented Dec 3, 2021 at 20:14
  • @Mike have you ever found a solution? Thanks! Commented Aug 15, 2022 at 13:28
  • @nrudnyk unfortunately not! I gave up and am using the default toolbar style from Apple for now. I haven't tried SwiftUI 3 yet, however, so it's possible that there is an opportunity with the new version coming out this fall. Commented Aug 17, 2022 at 7:58

3 Answers 3

0

I think the issue you're running into is that the toolbar has a "safe area" you need to explicitly ignore. This can be done with the following code.

.toolbar { 
     // your toolbar code 
}.background(Color.gray.ignoresSafeArea())

The toolbarBackground modifier only works for macOS 13.0 and above so it wouldn't be a general answer if you need backwards compatibility.

Sign up to request clarification or add additional context in comments.

2 Comments

Seems like a duplicate of the previous answer. The last bit might be a useful comment on that existing answer; you'll be able to comment once you build a small amount of reputation.
Unfortunately this has the same/no effect. It displays the same as the original post screenshots. If I apply this to the .toolbar modifier, the background takes up the entire view, not just the toolbar.
0

This is now possible with toolbarColorScheme(_:for:). See documentation here: https://developer.apple.com/documentation/swiftui/view/toolbarcolorscheme(_:for:)

Example usage:

    NavigationSplitView {
        // ...
    }
        .toolbarBackground(Color.accentColor)
        .toolbarColorScheme(.dark)

Example output: enter image description here

1 Comment

I wasn't sure how to get the separators to stay while using this method.
-1

I recommend

{
.toolbar {
      Toolbar()
    }.toolbarBackground(Color.gray)
}

2 Comments

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
This doesn't match the API signature at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.