4

I need to get this appearance of NSSearchField

But it seems to be impossible to set backgroundColor with standard methods of Interface Builder. How i can achieve this? Subclassing NSSearchField and NSSearchFieldCell didn't help, because i loose all animations (when you click on search field and icon+text sliding to left) when implementing my own draw methods.

surfrider
  • 1,376
  • 14
  • 29

1 Answers1

9

What your screenshot shows is just the standard look of NSSearchField when being placed under an NSVisualEffectView. That is, the search field will automatically get the look above when it's a subview of NSVisualEffectView (with the material of NSVisualEffectMaterialLight).

However, if you want this appearance without NSVisualEffectView, you can manually set its appearance:

searchField.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];

enter image description here

Renfei Song
  • 2,941
  • 2
  • 25
  • 30
  • Oh my god... That is. Thank you very much! I see no way that I would have guessed to look into NSVisualEffectView. – surfrider May 17 '15 at 14:25
  • Do i have a way to change colour of background to lighter? – surfrider May 17 '15 at 14:31
  • AFAIK, you cannot customize the appearance of `NSTextField` without touching the layer (as @Ckouta suggested in another answer) or subclassing it. If you are feeling you must customize it, I would suggest the subclassing way. – Renfei Song May 17 '15 at 14:35
  • Subclassing loose animations, i don't know why. Maybe i'm doing something wrong. I just overrided drawFrame or draw method and text with icon looses their animations on search field click. – surfrider May 17 '15 at 14:42
  • Also i discovered that `self.searchField.appearance = NSAppearance(named:NSAppearanceNameVibrantLight );` method and `NSVisualEffectView` (changed to vibrant light) are giving various results - in first case i got lighter colour. – surfrider May 17 '15 at 14:43
  • 1
    Well, under an `NSVisualEffectView`, `NSTextField` will change its fill color to reflect the background color (just as `NSVisualEffectView` does), so its color may vary a little at different situations. Regarding subclassing, it seems that `-drawInteriorWithFrame:inView:` of `NSTextFieldCell` handles the animation, but I am not quiet sure about how to implement this method while preserving the animation. That's another question. – Renfei Song May 17 '15 at 15:04