Customize NSToolbar - Disable “Use small size”

How do I disable the "Use small size" option in the toolbar? I am using Xcode 4.

(That's the option that appears when users go to customize the Toolbar.)

Answers


You could subclass NSToolbar, override -setSizeMode: and in your implementation call [super setSizeMode: NSToolbarSizeModeRegular];.

If you're instantiating the toolbar in Interface Builder then make sure you assign your subclass to the toolbar in the nib.

@implementation RKToolbar
- (void)setSizeMode:(NSToolbarSizeMode)aSizeMode
{
    [super setSizeMode:NSToolbarSizeModeRegular];
}
@end

This won't remove the checkbox from the customize panel but it will prevent it from doing anything.

There's not really a supported way to remove the checkbox. This does work but it's pretty hacky:

//in your NSToolbar subclass
- (void)runCustomizationPalette:(id)sender
{
    [super runCustomizationPalette:sender];

    NSWindow* toolbarWindow = [NSApp mainWindow];

    NSWindow* sheet = [toolbarWindow attachedSheet];

    for(NSView* view in [[sheet contentView] subviews])
    {
        if([view isKindOfClass:[NSButton class]])
        {
            if([[[(NSButton*)view cell] valueForKey:@"buttonType"] integerValue] == NSSwitchButton)
            {
                [view setHidden:YES];
            }
        }
    }
}

If you're not distributing on the Mac App Store, and don't mind subclassing private methods, you can create an NSToolbarSubclass and override _allowsSizeMode: to return NO:

- (BOOL)_allowsSizeMode:(NSToolbarSizeMode)mode {
    return mode != NSToolbarSizeModeSmall;
}

This has the added benefit of removing the checkbox from the customization sheet, as well.


Thanks to Rob Keniger for the excellent start. If you can have your custom toolbar as a delegate of your window, you can avoid having "Use small size" visible by getting at the sheet before it is displayed on screen. Do this by implementing [NSToolbar window:willPositionSheet:usingRect:] in the custom toolbar class. Elsewhere in your code, you'll need to do:

[myWindowWithToolbar setDelegate:myInstanceOfXXToolbar];

Here's the updated custom toolbar class:

@implementation XXToolbar

- (void)setSizeMode:(NSToolbarSizeMode)aSizeMode
{
    [super setSizeMode:NSToolbarSizeModeRegular];
}

- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect {
    NSView *buttonView = nil;

    for(NSView* view in [[sheet contentView] subviews])
    {
        if([view isKindOfClass:[NSButton class]])
        {
            if([[[(NSButton*)view cell] valueForKey:@"buttonType"] integerValue] == NSSwitchButton)
            {
                buttonView = view;
                break;
            }
        }
    }

    if (buttonView) {
        [buttonView setHidden:YES];

        // This is important as it causes the sheet to redraw without the button off screen
        [[sheet contentView] display];
    }

    return rect;
}

@end

Hope you find this useful.


Need Your Help

Transform data from header level to line level

sql oracle plsql

How do I transform a data from header level to line level with plsql?

java.lang.NullPointerException when populating Jtextfield using a bean class

java mysql swing netbeans javabeans

Here is my class which is a gui consisting of two tabs, my profile and edit profile. I am having a problem at the 'myProfileTabStateChanged' method when the index value changes to 1. When tab index...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.