2

I’m using a NSSearchField (not subclassed). When it’s not focused, the placeholderString is centered. But the lupe icon on the left appears a bit offset, so in fact the whole thing appears not to be centered.

Is it possible to hide the lupe icon?

enter image description here

ixany
  • 5,433
  • 9
  • 41
  • 65

2 Answers2

4

There is not any direct access to the icon, so that a workaround is first access to the NSSearchField cell (casted as shown), and afterwards access to its button cell.

self in this example is an instance of NSSearchField

[(NSButtonCell *)[(NSSearchFieldCell *)self.cell searchButtonCell] setTransparent:YES];

Deafault Search Field:

enter image description here

Hidden/Transparent Icon:

enter image description here

  • 1
    It may be better to add some description to your answer, to help others understand it. – zhm May 23 '17 at 16:03
  • I agree @MartinZhai. Hope the edited version clarifies it. – Daniel Gastón May 24 '17 at 09:14
  • Thank you Daniel! Works really great. However, I couldn’t find out how to hide the icon when the searchField is inactive but display it when the user is editing it. Any easy solution for that too? :) – ixany Jun 09 '17 at 07:56
1

Swift3 conversion:

(self.cell as! NSSearchFieldCell).searchButtonCell?.isTransparent = true
firstinq
  • 772
  • 6
  • 13