I may be getting lost in a glass of water as I am not an experienced developer but I cannot seem to be able to implement a simple override to modify the size of an NSTabView item.
- I have a Tab View Controller (Style = toolbar)
- I have a Tabless Tab View
- I have 3 Tab Items. For testing I have only subclassed one of them to the subclass below
I have created a new subclass of NSTabViewItem: MyTabViewItem and subclassed one of the 3 tab Items. The code is:
import Cocoa
class MyTabViewItem: NSTabViewItem {
override func drawLabel(_ shouldTruncateLabel: Bool, in labelRect: NSRect) {
var size = self.sizeOfLabel(false)
size.width = 180
print("Draw!!")
}
override func sizeOfLabel(_ computeMin: Bool) -> NSSize {
var size = super.sizeOfLabel(false)
size.width = 180
print("Draw!!")
return size
}
}
Everything works, except the subclassing. The Tabs appear, they do operate by switching the views and the program runs as it should. Except that it does not resize the Tab Item. The code in the subclass MyTabViewItem is never reached (it never prints Draw!! as it should.
I cannot understand what I am missing here. I have not read of any IB connection to make (and I cannot seem to be able to connect the Tab Items anyways). Please apologise if it isa trivial question but I have searched everywhere and not found anything to help me.
Thank you
You said:
This is your problem. An
NSTabViewonly asks anNSTabViewItemtodrawLabelif theNSTabViewitself is responsible for drawing the tab bar, but you have a “Tabless” tab view. (“Tabless” is the default style when you drag anNSTabViewControllerinto a storyboard.)You also said:
So you don't even want the tab view to draw a tab bar; you want items in the window toolbar to select tabs (like in Xcode's preference window).
Your ability to customize the toolbar items created for your tabs is limited. You can subclass
NSTabViewControllerand overridetoolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:, like this:But I found that making other changes didn't work well:
toolbarItem.imagedidn't work well for me.toolbarItem.viewmade the item stop receiving clicks.Note that the
minSizeandmaxSizeproperties are only used iftoolbarItem.viewis set.Your best bet is probably to manage the toolbar yourself, without trying to use
NSTabViewController's support.