I have this MWE right here -- it has a toolbar with a random action on it, in addition to a scroll view as the content of the window, with random labels attached inside. Since the redeisgn of the NSToolbar stuff in Tahoe, I expect the share button be able to "float" on top of the scrolled out content as shown as the first image at https://developer.apple.com/documentation/TechnologyOverviews/adopting-liquid-glass.
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSToolbarDelegate>
@property (strong) NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
NSRect frame = NSMakeRect(100, 100, 600, 400);
self.window = [[NSWindow alloc] initWithContentRect:frame
styleMask:(NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskResizable |
NSWindowStyleMaskMiniaturizable)
backing:NSBackingStoreBuffered
defer:NO];
[self.window setTitle:@"Scroll View + Toolbar Demo"];
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainToolbar"];
toolbar.displayMode = NSToolbarDisplayModeIconAndLabel;
toolbar.delegate = self;
[self.window setToolbar:toolbar];
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:self.window.contentView.bounds];
[scrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
NSView *documentView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 1000, 1000)];
for (int i = 0; i < 10; i++) {
NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(50, 950 - i*80, 400, 40)];
[label setStringValue:[NSString stringWithFormat:@"Sample Label #%d", i + 1]];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
[documentView addSubview:label];
}
[scrollView setDocumentView:documentView];
[self.window setContentView:scrollView];
[self.window makeKeyAndOrderFront:nil];
}
- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
return @[NSToolbarFlexibleSpaceItemIdentifier, @"ShareItem"];
}
- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
return @[@"ShareItem"];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
if ([itemIdentifier isEqualToString:@"ShareItem"]) {
NSToolbarItem *shareItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
shareItem.toolTip = @"Share this content";
shareItem.image = [NSImage imageNamed:NSImageNameShareTemplate];
shareItem.target = self;
shareItem.action = @selector(shareAction:);
return shareItem;
}
return nil;
}
- (void)shareAction:(id)sender {
NSLog(@"Share button clicked!");
// Here you could present a sharing service picker
NSSharingServicePicker *picker = [[NSSharingServicePicker alloc] initWithItems:@[@"Hello, world!"]];
[picker showRelativeToRect:[sender view].bounds ofView:[sender view] preferredEdge:NSRectEdgeMinY];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *delegate = [[AppDelegate alloc] init];
[app setDelegate:delegate];
[app run];
}
return EXIT_SUCCESS;
}
But it doesn't and produces this image:
https://imgur.com/a/kA7MzIe
I've tried to set various settings to make the top bar transparent, but all it does is that it makes it completely opaque instead. How can I make the share button float on top of the content?
P.S. the app is a single-file app, compile it with
clang -fobjc-arc -framework Cocoa -o ScrollApp toolbar.m