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.
}
}}
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 {
}
}
}
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()
}
}
MacOS Playground: How can change the background color?
You just need to set your view wantsLayer property to true
let view = views[0] as! NSView
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.blue.cgColor
How to color the title bar of the OS X app with swift
Alternative
If you want to only set a background color for the whole NSWindow and hide the title bar, you can set its styleMask to textured:
self.view.window?.styleMask = NSTexturedBackgroundWindowMask
self.view.window?.backgroundColor = NSColor.redColor()
The advantage of this: It is backwards compatible to at least OS X 10.6
Setting Background Color of NSTableCellView in Swift
If you just want the rows to use the standard alternating colors for rows, there's a simple checkbox in the Attributes inspector for the table view in IB to enable that.
To use a non-standard background color, you want to set the row view's backgroundColor
, but not inside of drawRect()
. If you change properties of a view that affect how it draws inside of drawRect()
, that will probably mark the view as needing display, which will provoke another call to drawRect()
, etc.
It should work to just set it in the delegate's tableView(_:didAddRowView:forRow:)
method. That's documented in the description of the backgroundColor
property.
With regard to your attempt at overriding drawRect()
: setting the row view's backgroundColor
will presumably affect how the superclass draws. So, setting it after calling through to super is unlikely to help. It definitely won't affect the subsequent NSRectFill()
call. That function relies on the fill color set for the current graphics context, which is implicit. You would change that by calling someColor.set()
.
Buy, anyway, there should be no need to override drawRect()
given that you can set the backgroundColor
. If you want to achieve some background drawing beyond what's possible by just setting a color, you should override drawBackgroundInRect()
and not drawRect()
, anyway.
Finally, your implementation of tableView(tableView:rowViewForRow:)
should call the table view's makeViewWithIdentifier(_:owner:)
method first, before creating a new view. And it should set the identifier on any new view it does create. That allows the table view to maintain a reuse queue of views, to avoid constantly destroying and recreating views.
How to create background for NSView when using storyboards with OS X
You shouldn't need a new viewController to change the background of an NSView. If you aren't subclassing NSView, then you can simply call:
myView.wantsLayer = true
myView.layer!.backgroundColor = CGColorGetConstantColor(kCGColorBlack)
I'm not sure if you can do it strictly in the storyboard, though you can likely set the wantsLayer = true in storyboard using the User Defined Runtime Attributes.
Related Topics
Difference Between Packed VS Normal Data Type
How to Set Inputview for Textfield in Swiftui
How to Get the Download Progress with the New Try Await Urlsession.Shared.Download(...)
Case Insensitive Dictionary in Swift
How to Check If a String Contains Chinese in Swift
Swiftui - How to Use Oncommand with Nsmenuitem on MACos
Swiftui: Dismiss View Within MACos Navigationview
How to Filter Events Created for the Current Date in the Realm Swift
Why Do Servicesubscribercellularproviders Return Nil? (In iOS 12)
How to Automatically Reflect Coredata+Icloud Changes in Swiftui View
How to Call Initializer for Subclass of Generic Type
Difference Between Dispatchqueue Types in Swift
How to Make a Uiview Focusable Using the Focus Engine on Apple Tv
List All Available Audio Devices
Uisplitviewcontroller in Portrait on iPhone Always Show Master and Detail in iOS 8
Update Core Data Object Order - Not Working
Dispatch_Once_T' Is Unavailable in Swift: Use Lazily Initialized Globals Instead