NSTextField in toolbar on Big Sur

Does anyone have a suggestion for adding a text field to a window's toolbar that'll resize with the window?

Before you say use an NSSearchField, I don't want the search and cancel buttons displayed and AFAIK, you can't hide those buttons in older versions of macOS which I still have to support. Also, I want the toolbar item to resize with the window. I've tried putting using an NSTextField as a custom view for an NSToolbarItem but for some reason, the text field appears a few pixels shorter and all text within it get cropped vertically. I filed a bug report during the Big Sur beta but got no response.


Accepted Reply

To answer my own question, you're supposed to use constraints. I actually had tried adding constraints earlier but Interface Builder wouldn't let me add any to my existing existing text field. I found out later that it worked with a newly added text field so I just used that one and deleted my old one.

You create a custom view and embed the text field in the custom view. You make the custom view the view for the toolbar item. You then add constraints such as align center X and align center Y. I added a constraint to set the text field height to 21. That fixed the problem where the toolbar item was shrinking my text field's height. You then add a constraint to make the text field the same width as the custom view. Finally, I added a constraint for its width and made it >= to the minimum size I wanted for the text field. Now my text field's width resizes as the window resizes.

Replies

I don't think toolbar items really support resizing. This thread has suggestions that someone received from Apple: https://developer.apple.com/forums/thread/73357

It is little more than a hack. I don't know how well it is going to work with Big Sur's new toolbars.

PS: In the future, don't wait for Apple to fix bugs. Report the bug in hopes that one day you will be able to remove your hack, or at least remove it on OS versions where it is fixed so you get more consistent behaviours. But once you find a bug, you'll have to work around the problem yourself and your fix will have to last until you drop support for the OS version(s) affected.
You can get a toolbar item to resize with the window if you set its maxSize to a high value which I've been doing for years. However, minSize and maxSize for toolbar items are deprecated in Big Sur. I was able to solve the text edit field height problem by using a height constraint but I still can't figure out how to get the edit field to resize its width with the window (or at least set it to use a larger default width). I could live with the current default size but I think I'm just going to live with the warnings that minSize and maxSize are deprecated until I can find a better solution.
To answer my own question, you're supposed to use constraints. I actually had tried adding constraints earlier but Interface Builder wouldn't let me add any to my existing existing text field. I found out later that it worked with a newly added text field so I just used that one and deleted my old one.

You create a custom view and embed the text field in the custom view. You make the custom view the view for the toolbar item. You then add constraints such as align center X and align center Y. I added a constraint to set the text field height to 21. That fixed the problem where the toolbar item was shrinking my text field's height. You then add a constraint to make the text field the same width as the custom view. Finally, I added a constraint for its width and made it >= to the minimum size I wanted for the text field. Now my text field's width resizes as the window resizes.