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.
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.
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.
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.