Layout 如何在窗口(视图)中固定图层

Layout 如何在窗口(视图)中固定图层,layout,calayer,autolayout,nswindow,Layout,Calayer,Autolayout,Nswindow,想象一下,在其父层(即支持NSView的层的根层)中绘制一个黄色框(CALayer实例) 当父视图的框架发生变化时,黄色框应保持在屏幕上的相同位置。当我将自动调整大小遮罩设置为NSViewMinXMarging和NSViewMaxXMarging时,屏幕上的黄色框在改变视图宽度之间按比例移动 如何在保持子层处于初始位置的同时调整NSWindow(NSView)的框架 我知道-(void)layoutSublayersOfLayer:(CALayer*)layerCALayerDelegate方法

想象一下,在其父层(即支持
NSView
的层的根层)中绘制一个黄色框(
CALayer
实例)

当父视图的框架发生变化时,黄色框应保持在屏幕上的相同位置。当我将自动调整大小遮罩设置为
NSViewMinXMarging
NSViewMaxXMarging
时,屏幕上的黄色框在改变视图宽度之间按比例移动

如何在保持子层处于初始位置的同时调整
NSWindow
NSView
)的框架

我知道
-(void)layoutSublayersOfLayer:(CALayer*)layer
CALayerDelegate
方法,但我不知道如何实现此行为

这里是演示:

这是我的方法。 子类NSWindow

.h文件:

#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>

@interface NNMainWindow : NSWindow <NSWindowDelegate> {

    CALayer* yellowLayer;

    NSRect leftEdge;
    NSRect rightEdge;
    NSRect topEdge;
    NSRect bottomEdge;
}
@end
#import "NNMainWindow.h"

@implementation NNMainWindow

- (void)awakeFromNib {

    //Add yellow layer here....

    [self setAcceptsMouseMovedEvents:YES];

    [self updateResizingMarkers];
}

- (void)updateResizingMarkers {
    NSSize horizontalRectSize = NSMakeSize(NSWidth(self.frame), 8.f);
    horizontalRectSize.width += 10.f;
    NSSize verticalRectSize = NSMakeSize(8.f, NSHeight(self.frame));
    verticalRectSize.height += 10.f;

    leftEdge = NSZeroRect;
    leftEdge.size = verticalRectSize;
    leftEdge.origin = NSMakePoint(-5.f, -5.f);

    rightEdge = NSZeroRect;
    rightEdge.size = verticalRectSize;
    rightEdge.origin = NSMakePoint(horizontalRectSize.width - 10.f - 3.f, -5.f);
}

- (void)mouseMoved:(NSEvent *)theEvent {

    NSPoint currentLocation = [theEvent locationInWindow];

    if (NSPointInRect(currentLocation, leftEdge))
        yellowLayer.autoresizingMask = kCALayerMinXMargin;

    if (NSPointInRect(currentLocation, rightEdge))
        yellowLayer.autoresizingMask = kCALayerMaxXMargin;
}

- (void)windowDidResize:(NSNotification *)notification {
    [self updateResizingMarkers];
}

@end