Search code examples
macoscocoafullscreenappkit

Position the search box at the left of the title bar


I'm using INAppStoreWindow for my application. I put an NSSearchButton in the title bar of the application.

INAppStoreWindow *aWindow = (INAppStoreWindow*)self.window;
aWindow.titleBarHeight = 60.0;
aWindow.showsTitle = YES;

NSView *titleBarView = aWindow.titleBarView;
NSSize buttonSize = NSMakeSize(200.f, 100.f);
NSRect buttonFrame =
    NSMakeRect(NSMidX(titleBarView.bounds) - (buttonSize.width / 2.f),
               NSMidY(titleBarView.bounds) - (buttonSize.height / 2.f),
               buttonSize.width,
               buttonSize.height);

The previous code positioned the search box at the center of the title bar. What I want to do is position the search box at the left side of the title bar and if "Maximize" button of the application is clicked, I want the search box to remain at left. (not exact left. I want the left where there is a space between the search box and the left edge of the title bar).

Of course, I want to leave a space for the "full screen" arrows.

I'm not really familiar with Cocoa development (yet).
How to position my search box as I want?


Solution

  • You will have to register to the notifications (NSWindowWillExitFullScreenNotification, NSWindowWillEnterFullScreenNotification) and adjust the frame of the button accordingly.

    For example

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(windowWillEnterFullScreen:)
                                                     name:NSWindowWillEnterFullScreenNotification
                                                   object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(windowWillExitFullScreen:)
                                                     name:NSWindowWillExitFullScreenNotification
                                                   object:nil];
    

    And later

    - (void)windowWillEnterFullScreen:(NSNotification *)notification
    {
     // set your button frame to the left
    }
    
    
    - (void)windowWillExitFullScreen:(NSNotification *)notification
    {
     // set your button frame back to the centre
    }
    

    Hope this helps