NSToolbar special area

ぃ、小莉子 提交于 2019-12-10 11:18:09

问题


I like to try to completely take over the area where the NSToolbar resides so I can put my own custom controls, views and background. The advantages of using this area are:

  • Any sliding panels appear below the toolbar area instead of just the title bar.
  • In Lion, the toolbar area comes down along with the menu bar when the mouse is at the top of the screen.

I have tried using a borderless window, and implementing my own custom views within it but unfortunately I lose the above advantages as well as having a few other minor problems.

My current method is to use the undocumented method '_toolbarView' with the NSToolbar and add my custom view into its subviews. This works fine as I can turn off toolbar customisation. Unfortunately, the size of the toolbar is initialised with the items within that toolbar. Does anyone know if I can change the size of toolbar without adding a fake ToolbarItem?

Maybe there's also a better way of doing this that I am currently unaware of. Thanks for any suggestions and comments.


回答1:


No need to use any undocumented APIs. Just create a toolbar item with a custom view:

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
    NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
    …
    [item setView:myCustomToolbarView];
    …
}

You can control your custom toolbar’s size using the item’s minSize and maxSize properties (e. g. in your NSWindowDelegate’s -windowDidResize:).

Remember to also update the toolbar display mode so it doesn't show item labels:

[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];


来源:https://stackoverflow.com/questions/7058046/nstoolbar-special-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!