将搜索框放在标题栏的左侧

时间:2014-04-02 20:34:53

标签: macos cocoa fullscreen appkit

我正在使用INAppStoreWindow申请。我在应用程序的标题栏中放了一个NSSearchButton

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);

前面的代码将搜索框放在标题栏的中心。我想要做的是将搜索框放在标题栏的左侧,如果点击了应用程序的“Maximize”按钮,我希望搜索框保持在左侧。 (不完全是左边。我希望左边的搜索框和标题栏的左边缘之间有空格。)

当然,我想为“全屏”箭头留出空间。

我对Cocoa开发并不熟悉(尚) 如何根据需要定位我的搜索框?

1 个答案:

答案 0 :(得分:1)

您必须注册通知(NSWindowWillExitFullScreenNotification,NSWindowWillEnterFullScreenNotification)并相应地调整按钮的框架。

例如

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

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

以后

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


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

希望这有帮助