Setting Backgroundcolor of Custom Nsview

Setting backgroundColor of custom NSView

The correct way is

class ViewController: NSViewController {

@IBOutlet var box: NSView!

override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true

}

override func viewWillAppear() {
box.layer?.backgroundColor = NSColor.blue.cgColor
//box.layer?.setNeedsDisplay()
}

override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}}

Best way to change the background color for an NSView

Yeah, your own answer was right. You could also use Cocoa methods:

- (void)drawRect:(NSRect)dirtyRect {
// set any NSColor for filling, say white:
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}

In 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()
}

}

Set background color of NSView and NSImageView in cocoa

Their are two way to set background color of NSView/NSImageView, which i found.

First: By Subclassing of NSView/NSImageView

- (void)drawRect:(NSRect)aRect
{
[[NSColor redColor] set];
NSRectFill([self bounds]);
}

Second:

Don't want to make subclass, as you mentioned in your question.
Then

[_backgroundView setWantsLayer:YES];
[_backgroundView.layer setBackgroundColor:[[NSColor redColor] CGColor]]];

Here _backgroundView is the IBOutlet/object of NSView/NSImageView. You just need to access layer of NSView/NSImageView for giving him backgroundcolor without subclassing them.

How to set the background color of Customview(NSView)in mac applications?


  self.titleBarView.layer.backgroundColor = [NSColor redColor].CGColor;

If you do not want to use view.layer. You can use NSBox (with custom style) over NSView. NSBox has fillColor property.

How to set color of NSView in Cocoa App using Swift?

You should set wantsLayer to true for your subviews, not for your superview.

self.topView.wantsLayer = true
self.rightView.wantsLayer = true
self.leftView.wantsLayer = true

How to change background color for NSview in Cocoa

Welcome to Cocoa drawing.
Cocoa drawing uses Quartz which is a PDF model.
Drawing in this occurs in a back to front procedural order.

In Quartz drawing there is a drawing environment state object called the Graphics Context.
This is an implicit object in many of the drawing ops in AppKit.
(in Core Graphics or other APIs it could need to be explicitly called)

You tell the Graphics Context what the current color and other parameters are, then draw something, then change parameters and draw more, etc...
In AppKit, you do this by sending a message to the NSColor object, which is weird. but that's how it works.

In your drawRect: method you should call super first usually, because you probably want your drawing on top of that...

- (void) drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

// This next line sets the the current fill color parameter of the Graphics Context
[[NSColor whiteColor] setFill];
// This next function fills a rect the same as dirtyRect with the current fill color of the Graphics Context.
NSRectFill(dirtyRect);
// You might want to use _bounds or self.bounds if you want to be sure to fill the entire bounds rect of the view.
}

If you want to change the color, you'll need an @property NSColor
You might need more than one for your drawing.

That allows you to set the color.

You might want the view to use KVO and observe its own color property then draw itself if the color property changes.

You could do a lot of different things to set the color. (a button or pallette elsewhere) But all of them would eventually result in sending a message to set the color of a property of your view for drawing.

Finally, if you want to update the drawing, you need to call [myView setNeedsDisplay:YES]; where myView is a reference to an instance of the NSView subclass.
There is also display but that's forceful.
setNeedsDisplay: says to schedule it on the next run of the event loop (runLoop). display kind of makes everything jump to that right away.
The event loop comes back around fast enough you shouldn't force it.
Of note, setNeedsDisplay: is the entire view.
In a fancy ideal world with complex views, you might want to more appropriately optimize things by calling setNeedsDisplayInRect: where you designate a specific CG/NSRect of the view as needing to be redrawn.
This allows the system to focus redrawing to the smallest union rect possible in the window.

Does NSView have a backgroundColor? If it does, I can't find it

They must be thinking of UIView, which does have a backgroundColor property. NSView does not have a backgroundColor property.

You will have to achieve your effect some other way, e.g., through subclassing NSView.

Changing background color of NSView in Swift 2.0


import Cocoa

class ViewController: NSViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.wantsLayer = true
view.layer?.backgroundColor = NSColor(red: 1, green: 0, blue: 0, alpha: 1).CGColor

}

override var representedObject: AnyObject? {
didSet {

}
}
}

Setting the background color of an NSView

It's best to use the already-made setBackground: method that you get from the background property. So replace your changeColor: method with:

-(void)setBackground:(NSColor *)aColor
{
if([background isEqual:aColor]) return;
[background release];
background = [aColor retain];

//This is the most crucial thing you're missing: make the view redraw itself
[self setNeedsDisplay:YES];
}

To change the color of your view, you can simply do:

self.homeView.background = [NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]


Related Topics



Leave a reply



Submit