Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to do something similar. ITSearchField is very solid and nice. However, I desired to expand the search field once it gained focus. As one might find out quickly the NSSearchfield is comprised of many components and layers.</p> <p>Here is what I wrote to get it to work... Note: It works on 10.7 or greater. Also you must hook-up the layout constraint of the search field control's width in IB to the outlet.</p> <p>.h</p> <pre><code>@interface MySearchField : NSSearchField @end </code></pre> <p>.m</p> <pre><code>#pragma mark - Implementation: Search Field Control @interface MySearchField() @property (readwrite, nonatomic, getter=isExpanded) BOOL expand; @property (readwrite, nonatomic, getter=hasFocusRing) BOOL focusRing; @property (readwrite, strong, nonatomic) NSTimer *expandTimer; @property (readwrite, strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint; @property (readwrite, nonatomic) CGFloat searchWidth; @end static NSTimeInterval const kSearchFieldTime = 0.5; @implementation MySearchField @synthesize expand, focusRing; @synthesize expandTimer, searchWidth; @synthesize widthConstraint; #pragma mark Timer Methods: - (void) resizeSearchField { if ( self.hasFocusRing ) { if ( !self.isExpanded ) { self.searchWidth = self.widthConstraint.constant; [[self.widthConstraint animator] setConstant:(self.searchWidth * 2)]; self.expand = YES; } } else { if ( (self.isExpanded) &amp;&amp; ((!self.stringValue) || ([self.stringValue isEqualToString:@""])) ) { [[self.widthConstraint animator] setConstant:self.searchWidth]; self.expand = NO; } } } - (void) stopTimer { if ( self.expandTimer ) [self.expandTimer invalidate]; self.expandTimer = nil; } - (void) startTimer { [self stopTimer]; SEL resizeSelector = @selector(resizeSearchField); NSMethodSignature *expandSignature = [MySearchField instanceMethodSignatureForSelector:resizeSelector]; NSInvocation *expandInvocation = [NSInvocation invocationWithMethodSignature:expandSignature]; [expandInvocation setSelector:resizeSelector]; [expandInvocation setTarget:self]; self.expandTimer = [NSTimer scheduledTimerWithTimeInterval:kSearchFieldTime invocation:expandInvocation repeats:NO]; } #pragma mark Override Methods: - (void) noteFocusRingMaskChanged { self.focusRing = !NSEqualRects(NSZeroRect, self.focusRingMaskBounds); [self startTimer]; [super noteFocusRingMaskChanged]; } @end </code></pre> <p>Ok that's it really. You get notified when the focus ring changes and start a quick timer method. The timer is necessary for when you click the search field's cancel button which resets the control. The only thing the timer does is fire the resizeSearchField method.</p> <p>Update: Oh yeah, don't forget to set the class of your Search Field control in IB to MySearchField or whatever you want to call it.</p> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload