Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Yosemite NSSearchField Fixes
// 1. Disable centered look & animation:
// 1.1. Subclass NSSearchFieldCell and add:
- (BOOL)isCenteredLook
{
return NO;
}
// 1.2. Subclass NSSearchField and add:
- (void)setWantsLayer:(BOOL)wantsLayer
{
}
- (BOOL)wantsLayer
{
return NO;
}
- (CALayer *)makeBackingLayer
{
return nil;
}
// 2. To fix cursor not blinking when the search field becomes first responder,
// subclass NSSearchField and add:
- (BOOL)becomeFirstResponder
{
BOOL result = [super becomeFirstResponder];
if(result)
{
[self ensureCursorBlink];
}
return result;
}
- (void)selectText:(id)sender
{
[self ensureCursorBlink];
[super selectText:sender];
}
- (void)ensureCursorBlink
{
if(isYosemite && !self.stringValue.length)
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(!self.stringValue.length)
{
[self setStringValue:@" "];
[self setStringValue:@""];
}
});
}
}
// 3. If you're having issues updating the placeholder or image, this seems to cause the
// search field to update properly. This used to be needed in the GM, but no longer is
// the case in the final release (at least not with the not-centered look). YMMV:
[searchField setHidden:YES];
[searchField setHidden:NO];
// There might be other ways to do this. Note: this will also make the search field lose
// first responder, so make sure to restore it if needed.
// 4. I have a feeling I'm forgetting something, let me know if you encounter any issues.

fzwo commented Sep 16, 2015

The ensureCursorBlink is causing (rare) crashes for us on both 10.10 and 10.9. Maybe also 10.11, but we don't have many users on ElCap yet.

sfsam commented Aug 9, 2017

The above didn't work for me on 10.12. I'm using this which is a lot shorter and seems to work even though I don't understand why it works. Subclass NSSearchField:

- (NSRect)rectForSearchTextWhenCentered:(BOOL)isCentered {
    return [super rectForSearchTextWhenCentered:YES]; 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment