Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:43:57+00:00 2026-05-27T21:43:57+00:00

I have a search field whose original width is small (because it looks better

  • 0

I have a search field whose original width is small (because it looks better that way). However, when the user clicks on the search field, I want to make its width bigger by a certain amount. When the user clicks outside, I want to resize it to the original size. I also want to do all the resizes with animations.

Here is what I have so far:

#define kAddedWidth 85

@implementation ToolbarSearchField

- (id)initWithFrame: (NSRect)frameRect
{
    self = [super initWithFrame: frameRect];
    if (self) {
        originalSize = self.frame.size.width;
    }

    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    originalSize = self.frame.size.width;
}

- (BOOL)becomeFirstResponder
{    
    NSRect newFrame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, originalSize + kAddedWidth, self.frame.size.height);    
    [[self animator] setFrame: newFrame];

    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{    
    NSRect newFrame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, originalSize, self.frame.size.height);
    [[self animator] setFrame: newFrame];

    return [super resignFirstResponder];
}

@end

originalSize is an iVar.

The problem is that this isn’t resizing anything. Clicking on the field or outside isn’t doing a single bit.

I did some NSLog debugging and it seems that when I click on the field becomeFirstResponder gets called and immediately after that resignFirstResponder is called, nullifying the resize animation. However, the field is still active and blinking the insertion point. Have any ideas?

The Solution:

Thanks to Peter Hosey’s answer, I managed to solve this. Here is my solution:

#define kAddedWidth (self.superview.bounds.size.width - self.frame.origin.x * 2 - originalSize)

@implementation ToolbarSearchField

- (id)initWithFrame: (NSRect)frameRect
{
    self = [super initWithFrame: frameRect];
    if (self) {
        originalSize = self.frame.size.width;
    }

    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    originalSize = self.frame.size.width;
}

- (void)textDidBeginEditing: (NSNotification*)notification
{    
    [super textDidBeginEditing: notification];
    isEditing = YES;

    NSRect newFrame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, originalSize + kAddedWidth, self.frame.size.height);
    [[self animator] setFrame: newFrame];
}
- (void)textDidEndEditing: (NSNotification*)notification
{    
    [super textDidEndEditing: notification];
    isEditing = NO;

    if ([self.stringValue isEqualToString: @""]) {
        NSRect newFrame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, originalSize, self.frame.size.height);
        [[self animator] setFrame: newFrame]; 
    }
}

- (void)mouseDown: (NSEvent*)theEvent
{
    if (!isEditing) {
        NSRect newFrame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, originalSize + kAddedWidth, self.frame.size.height);
        [[self animator] setFrame: newFrame];
    }

    [super mouseDown: theEvent];
}

Whenever the field is clicked and it isn’t editing already, animate to a bigger size. Whenever the field ends editing, animate only if it doesn’t have text. If it has text then it is better not to make the field smaller.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:43:57+00:00Added an answer on May 27, 2026 at 9:43 pm

    Fields don’t handle text input. That responsibility goes to the field editor, which is an instance of NSText (or of some subclass thereof) owned by the window.

    You might try implementing the textDidBeginEditing: method and the textDidEndEditing: method instead.

    A search for “field editor” turns up several relevant results in several different programming guides.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that lets the user search over a number of fields
I would like to have one input field, where user will input search data.
I have a search box that will search for a keyword in the field
So my goal here is to have a single search field in an application
I currently have an advanced search page that searches a number of fields in
Currently I have a table that I search upon 4 fields, FirstName, LastName, MiddleName,
I have a search field. I want to show the results in a dialogue
I have an index that stores birth-dates, and I would like to search for
I have created a complex sql server 2008/coldfusion search page, that searches thru a
I decided that I wanted a search field on my website, the thing is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.