Search code examples
swiftcocoadrawingsubclassnssearchfield

NSSearchField: How to hide icon and border?


This is kind of a duplicate of this question. Because everything I know about Swift is Swift3, I`m wondering if someone could "translate" the suggested solution in this answer.

Also:

I made a NSSearchfield without border, put it in a framed view, and it still shows the gray border. I would be curious of how to disable the animated gray border and maybe even how to change the color of the gray "search" line.

My ugly result now looks like this:

enter image description here

It would be a big help if someone could tell me how to manage this difficult NSSearchfield.

//UPDATE

According to firstinq´s answer, the icon now disappeared, which is great. But still, there is this disturbing animated gray border. Which I can´t understand: The NSSearchFielt is inside a NSView (blue border). So everything outside the NSView should be hidden, right?. So why am I still seeing the gray border? cell.isBordered = falsehas no effect. Any advice how to handle that?

enter image description here

This is how I draw the border of the NSView:

class SearchFieldBorder: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        self.layer?.borderWidth = 1
        self.layer?.borderColor = NSColor.blue.cgColor
    }
}

Solution

  • To hide the icon: cast the cell to NSSearchFieldCell and set the cell's searchButtonCell to transparent. Possible swift3 version:

    if let cell = self.searchField.cell as? NSSearchFieldCell {
        cell.searchButtonCell?.isTransparent = true
    }
    

    Here searchField is an NSSearchField

    To remove the focus border:

    searchField.focusRingType = .none
    

    To change grey line/cursor it would be better to subclass the NSSearchField and override the methods. You can get an idea from here.