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

前端 未结 4 1686
余生分开走
余生分开走 2021-02-05 15:32

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 ac

4条回答
  •  攒了一身酷
    2021-02-05 16:23

    Inspired by your first approach I managed to get a toolbar too. As I'm using Divider()s in it, your Paddings didn't work great for me.

    Picture showing wrong alignment

    This one seems to work a bit smoother with different Layout-Sizes:

     let titlebarAccessoryView = TitlebarAccessory().padding([.leading, .trailing], 10).edgesIgnoringSafeArea(.top)
    
        let accessoryHostingView = NSHostingView(rootView:titlebarAccessoryView)
        accessoryHostingView.frame.size.height = accessoryHostingView.fittingSize.height+16
        accessoryHostingView.frame.size.width = accessoryHostingView.fittingSize.width
    

    Result with the right paddings

    Maybe there is an even smoother way to get rid of this +16 and the padding trailing and leading (there are several other options instead of fittingSize), but I couldn't find any that looks great without adding numerical values.

提交回复
热议问题