Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change NSTextField background color in NSPopOver

enter image description here

Mac OSX 10.10 Xcode 6.1

I created a tableview in NSPopOver. I try to change textfield's background color. Why? no effect. The tableview's highlight set to "regular". which way can let me change textfields background color to white?

like image 815
CocoaUser Avatar asked Mar 16 '15 10:03

CocoaUser


3 Answers

In my app, I had same problem. I used Swift and this worked for me. In your viewForTableColumn:

let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier!, owner: self) as! NSTableCellView
    cell.textField?.drawsBackground = true
    cell.textField?.backgroundColor = NSColor.clearColor()
like image 89
Prontto Avatar answered Sep 19 '22 14:09

Prontto


There's a known bug with text fields and the "vibrancy" blending added in Yosemite. It's known to affect popovers.

The workaround is to set the appearance property of the table view to NSAppearanceNameAqua.

This was confirmed by an Apple engineer in their devforums.

2019-05-09 EDIT:

This issue also sometimes affects NSTextFields that appear on popovers where the background is grey. Here's the Swift 5 fix, add this to the viewDidLoad() function of your popover controller

self.someTextField.appearance = NSAppearance.init(named: .aqua)

like image 28
Ken Thomases Avatar answered Sep 20 '22 14:09

Ken Thomases


I really like @Prontto's solution but it does not work with NSImageView because it has no drawsBackground or backgroundColor.

Fortunately the appearance option works for image views as well!

cell.imageView?.image = image ?? nil
cell.imageView?.appearance = NSAppearance(named: NSAppearanceNameAqua)
like image 41
asdf Avatar answered Sep 16 '22 14:09

asdf