I want to draw my own tabs for NSTabViewItem
s. My Tabs should look different and start in the top left corner and not centered.
How can I do this?
I want to draw my own tabs for NSTabViewItem
s. My Tabs should look different and start in the top left corner and not centered.
How can I do this?
it is possible to set the NSTabView's style to Tabless and then control it with a NSSegmentedControl that subclasses NSSegmentedCell to override style and behavior. For an idea how to do this, check out this project that emulates Xcode 4 style tabs: https://github.com/aaroncrespo/WILLTabView/.
NSTabView isn't the most customizable class in Cocoa, but it is possible to subclass it and do your own drawing. You won't use much functionality from the superclass besides maintaining a collection of tab view items, and you'll end up implementing a number of NSView and NSResponder methods to get the drawing and event handling working correctly.
It might be best to look at one of the free or open source tab bar controls first, I've used PSMTabBarControl in the past, and it was much easier than implementing my own tab view subclass (which is what it was replacing).
I've recently done this for something I was working on.
I ended using a tabless tab view and then drawing the tabs myself in another view. I wanted my tabs to be part of a status bar at the bottom of the window.
You obviously need to support mouse clicks which is fairly easy, but you should make sure your keyboard support works too, and that's a little more tricky: you'll need to run timers to switch the tab after no keyboard access after half a second (have a look at the way OS X does it). Accessibility is another thing you should think about but you might find it just works—I haven't checked it in my code yet.
One of possible ways to draw tabs - is to use NSCollectionView. Here is Swift 4 example:
Class
TabViewStackController
containsTabViewController
preconfigured with style.unspecified
and customTabBarView
.Class
TabBarView
containsCollectionView
which represents tabs.Class
TabViewController
preconfigured with style.unspecified
Rest of the classes.
Here is how it looks like:
I very much got stuck on this - and posted NSTabView with background color - as the PSMTabBarControl is now out of date also posted https://github.com/dirkx/CustomizableTabView/blob/master/CustomizableTabView/CustomizableTabView.m
It's very easy to use a separate
NSSegmentedCell
to control tab selection in anNSTabView
. All you need is an instance variable that they can both bind to, either in the File's Owner, or any other controller class that appears in your nib file. Just put something like this in the class Interface declaraton:Then, in the IB Bindings Inspector, bind the Selected Index of both the
NSTabView
and theNSSegmentedCell
to the sameselectedTabIndex
property.That's all you need to do! You don't need to initialize the property unless you want the default selected tab index to be something other than zero. You can either keep the tabs, or make the
NSTabView
tabless, it will work either way. The controls will stay in sync regardless of which control changes the selection.