如何以编程方式将分隔符放在NSView
上?我试过这个:
NSView *right = [[NSView alloc] initWithFrame:CGRectMake(319, 0, 1, self.view.frame.size.height)];
[self.view addSubView:right];
但它不起作用,请说明此代码段有什么问题,或者是否有其他方法可以执行此操作
答案 0 :(得分:0)
NSView *right = [[NSView alloc] initWithFrame:CGRectMake(319, 0, 1, self.view.frame.size.height)];
[right.layer setBackgroundColor:[NSColor blackColor].CGColor];
[self.view addSubView:right];
我认为那是因为你添加了right
,但你看不到它。尝试设置背景颜色。
答案 1 :(得分:0)
您有两种选择:
1)为self.view对象创建自己的NSView子类。然后在该子类中定义drawRect方法以绘制分隔符:
- (void)drawRect:(NSRect)r {
[[NSColor blackColor] set];
NSRectFill(NSMakeRect(319., 0, 1., self.view.bounds.size.height));
}
2)或者,您可以创建具有backgroundColor属性的自定义NSView子类。然后在该NSView子类中创建一个drawRect,用该颜色填充整个视图:
- (void)drawRect:(NSRect)r {
[self.backgroundColor set];
NSRectFill(r);
}
答案 2 :(得分:0)
您可以使用NSBox
绘制分隔线。
这是我建议您这样做的方式:
// Make the box one point wide, spanning the right edge of the view.
let viewBounds = view.bounds
let box = NSBox(frame:
CGRect(x: viewBounds.maxX - 1, y: viewBounds.minY,
width: 1, height: viewBounds.maxY))
// Tell AppKit to stretch the box vertically when the view resizes,
// and to keep it against the right edge of the view.
box.autoresizingMask = [.height, .minXMargin]
// Set the appearance of the box to solid red.
box.boxType = .custom
box.fillColor = .red
box.borderType = .noBorder
view.addSubview(box)