3

I have a macOS app I have created. And I have a Preferences window built using a swift & storyboard with a tabless Toolbar styled NSTabViewController.

I see no way to override the positioning of the tab icons though - they default to the left side of the window. Is it possible?

enter image description here

gypsyDev
  • 1,232
  • 1
  • 14
  • 22

4 Answers4

3

Subclass NSTabViewController and override func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier]. Add a flexibleSpace toolbar item at both sides.

Willeke
  • 14,578
  • 4
  • 19
  • 47
1

following Willeke's tip, I came up with this override in my NSTabViewController subclass:

override func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {

    super.toolbarDefaultItemIdentifiers(toolbar)

    var arr = Array<NSToolbarItem.Identifier>()
    for item in self.tabViewItems {
        if let identifier = item.identifier {
            arr.append(NSToolbarItem.Identifier.init(identifier as! String))
        }
    }

    //insert flexible spaces at first and last index
    arr.insert(NSToolbarItem.Identifier.flexibleSpace, at: 0)
    arr.append(NSToolbarItem.Identifier.flexibleSpace)

    return arr
}
gypsyDev
  • 1,232
  • 1
  • 14
  • 22
0

OC:

-(NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar{
    NSMutableArray *arr = [[super toolbarDefaultItemIdentifiers:toolbar] mutableCopy];
    [arr insertObject:[[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarFlexibleSpaceItemIdentifier].itemIdentifier atIndex:0];
    [arr addObject:[[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarFlexibleSpaceItemIdentifier].itemIdentifier];
    return arr;
}
coletrain
  • 2,809
  • 35
  • 43
mapboo
  • 1
  • 1
0

Swift:

override func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    var array = super.toolbarDefaultItemIdentifiers(toolbar)
    array.insert(NSToolbarItem.Identifier.flexibleSpace, at: 0)
    array.append(NSToolbarItem.Identifier.flexibleSpace)
    return array
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
mapboo
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 17 '22 at 07:41