Bad*_*ate 142 macos objective-c background-color nsview
我在寻找改变的最佳途径backgroundColor
的NSView
.我也希望能够设置适当的alpha
面具NSView
.就像是:
myView.backgroundColor = [NSColor colorWithCalibratedRed:0.227f
green:0.251f
blue:0.337
alpha:0.8];
Run Code Online (Sandbox Code Playgroud)
我注意到NSWindow
有这种方法,我不是这个NSColorWheel
或NSImage
背景选项的忠实粉丝,但如果他们是最好的,愿意使用.
moh*_*enr 131
是的,你自己的答案是对的.您还可以使用Cocoa方法:
- (void)drawRect:(NSRect)dirtyRect {
// set any NSColor for filling, say white:
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
Run Code Online (Sandbox Code Playgroud)
在Swift中:
class MyView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// #1d161d
NSColor(red: 0x1d/255, green: 0x16/255, blue: 0x1d/255, alpha: 1).setFill()
dirtyRect.fill()
}
}
Run Code Online (Sandbox Code Playgroud)
lor*_*isi 113
一种简单有效的解决方案是将视图配置为使用Core Animation层作为其后备存储.然后,您可以使用-[CALayer setBackgroundColor:]
设置图层的背景颜色.
- (void)awakeFromNib {
self.wantsLayer = YES; // NSView will create a CALayer automatically
}
- (BOOL)wantsUpdateLayer {
return YES; // Tells NSView to call `updateLayer` instead of `drawRect:`
}
- (void)updateLayer {
self.layer.backgroundColor = [NSColor colorWithCalibratedRed:0.227f
green:0.251f
blue:0.337
alpha:0.8].CGColor;
}
Run Code Online (Sandbox Code Playgroud)
而已!
JZA*_*ZAU 73
如果您是故事板爱好者,这里有一种不需要任何代码的方法.
NSBox
作为子视图添加到NSView并调整NSBox的框架与NSView相同.
在Storyboard或XIB中,将标题位置更改为无,将框类型更改为自定义,将边框类型更改为"无",将边框颜色更改为您喜欢的任何内容.
这是一个截图:
这是结果:
Gab*_*iel 32
如果首先将WantsLayer设置为YES,则可以直接操作图层背景.
[self.view setWantsLayer:YES];
[self.view.layer setBackgroundColor:[[NSColor whiteColor] CGColor]];
Run Code Online (Sandbox Code Playgroud)
Bad*_*ate 26
想想我是怎么做到的:
- (void)drawRect:(NSRect)dirtyRect {
// Fill in background Color
CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context, 0.227,0.251,0.337,0.8);
CGContextFillRect(context, NSRectToCGRect(dirtyRect));
}
Run Code Online (Sandbox Code Playgroud)
gre*_*use 22
我经历了所有这些答案,不幸的是,它们都没有为我工作.但是,经过大约一个小时的搜索,我发现这种非常简单的方式:)
myView.layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0, 0.9);
Run Code Online (Sandbox Code Playgroud)
Leo*_*bus 20
编辑/更新:Xcode 8.3.1•Swift 3.1
extension NSView {
var backgroundColor: NSColor? {
get {
guard let color = layer?.backgroundColor else { return nil }
return NSColor(cgColor: color)
}
set {
wantsLayer = true
layer?.backgroundColor = newValue?.cgColor
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
let myView = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
print(myView.backgroundColor ?? "none") // NSView's background hasn't been set yet = nil
myView.backgroundColor = .red // set NSView's background color to red color
print(myView.backgroundColor ?? "none")
view.addSubview(myView)
Run Code Online (Sandbox Code Playgroud)
最佳方案:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.wantsLayer = YES;
}
return self;
}
- (void)awakeFromNib
{
float r = (rand() % 255) / 255.0f;
float g = (rand() % 255) / 255.0f;
float b = (rand() % 255) / 255.0f;
if(self.layer)
{
CGColorRef color = CGColorCreateGenericRGB(r, g, b, 1.0f);
self.layer.backgroundColor = color;
CGColorRelease(color);
}
}
Run Code Online (Sandbox Code Playgroud)
使用 NSBox,它是 NSView 的子类,让我们可以轻松地设置样式
斯威夫特 3
let box = NSBox()
box.boxType = .custom
box.fillColor = NSColor.red
box.cornerRadius = 5
Run Code Online (Sandbox Code Playgroud)
毫无疑问,最简单的方法也与Color Set Assets兼容:
斯威夫特:
view.setValue(NSColor.white, forKey: "backgroundColor")
Run Code Online (Sandbox Code Playgroud)
目标-C:
[view setValue: NSColor.whiteColor forKey: "backgroundColor"];
Run Code Online (Sandbox Code Playgroud)
界面生成器:
backgroundColor
在类型为的界面构建器中添加用户定义的属性NSColor
。
只需backgroundColor
在图层上设置(在支持视图图层之后)。
view.wantsLayer = true
view.layer?.backgroundColor = CGColor.white
Run Code Online (Sandbox Code Playgroud)
在Swift中:
override func drawRect(dirtyRect: NSRect) {
NSColor.greenColor().setFill()
NSRectFill(dirtyRect)
super.drawRect(dirtyRect)
}
Run Code Online (Sandbox Code Playgroud)
我测试了以下内容,它对我有用(在 Swift 中):
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.blackColor().colorWithAlphaComponent(0.5).CGColor
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
111275 次 |
最近记录: |