可可:如何使图标一直依赖于右边?

时间:2012-04-22 05:06:02

标签: objective-c macos cocoa

此窗口状态栏。如何使图标一直依赖于右边?调整窗口大小,它自动向右移动。

enter image description here

-(void)composeInterface{
    NSView *themeFrame=[[self.window contentView] superview];
    NSRect themeFrameRect = [themeFrame frame];
    NSRect accessoryViewFrame =[self.statusBarBtn frame];
    NSRect newFrame = NSMakeRect(30,
                                 themeFrameRect.size.height - accessoryViewFrame.size.height,
                                 accessoryViewFrame.size.width,
                                 accessoryViewFrame.size.height);

    [self.statusBarBtn setFrame:newFrame];
//    [self.statusBarBtn setFrameOrigin:NSMakePoint(0, 0)];
//    [self.statusBarBtn setAutoresizingMask:323];

    [themeFrame addSubview:self.statusBarBtn];
}

1 个答案:

答案 0 :(得分:1)

如果您希望您的图标与右侧对齐您需要更改它的自动调整以对齐。您需要在 尺寸检查器 中执行此操作:

Size Inspector

或者像这样使用-setAutoresizingMask

[self.statusBarBtn setAutoresizingMask:333]; //NOT 323


此外,您newFrame的x possition设置为30,因此图标将位于左侧。

更改此内容:

NSRect newFrame = NSMakeRect(30,
                             themeFrameRect.size.height - accessoryViewFrame.size.height,
                             accessoryViewFrame.size.width,
                             accessoryViewFrame.size.height);

对此:

NSRect newFrame = NSMakeRect(themeFrameRect.size.width - accessoryViewFrame.size.width,
                             themeFrameRect.size.height - accessoryViewFrame.size.height,
                             accessoryViewFrame.size.width,
                             accessoryViewFrame.size.height);