Make UIBarButtonItem with customView as a Flexible-Space element

I have a UIBarButtonItem that has a UITextField as its customView. The toolbar contains this element and several buttons.

When resizing a toolbar (say, changing a device’s orientation), I want the UITextField increase or decrease (width) to occupy all the available space on the toolbar.

Without figuring out the combined sizes of all the buttons on the toolbar, and then, respectively, set the width of the UITextField , is there just some magic way to make this element behave like a flexible space element?

+13
source share
3 answers

If you set UIViewAutoresizingFlexibleWidth to a text field, it should automatically decrease or grow.

+2
source

So, as I wrote in a comment, the solution proposed by @Krishna_K does not always work.

Since I wrote my own method, I thought about sharing. Part of it is based on: Get the width of a UIBarButtonItem

 // Expand the status label to fill remaining space CGFloat totalItemsWidth = 0.0; CGFloat itemsMargin = 8.0; for (UIBarButtonItem *barButtonItem in myToolbar.items) { if (barButtonItem != self.myLabelItem) { // Get width of bar button item (hack from other SO question) UIView *view = [barButtonItem valueForKey:@"view"]; CGFloat width = view? [view frame].size.width : (CGFloat)0.0; totalItemsWidth += width + itemsMargin; } } self.myLabelItem.width = (myToolbar.frame.size.width - totalItemsWidth) - itemsMargin; 

Comments:

  • It is impossible to explain why this works with flexible spaces, but it

  • Please note that the elements passed to the UIToolbar are not saved, so you should always work with the elements of the panel directly from the toolbar, and not with the locally stored object.

  • In this hack there is the only thing that worked to get the width (the try width property, after some delay with which the views were added, tried to allow the extensions to be viewed on their own). The usual hacking warnings apply ...

What does he decide ...

+7
source

I have a nice subclass of UIToolbar that I just wrote to do this, thanks to the code in the Oded post. It works great for a toolbar with 5 elements: button, flexible space, text box, flexible space, button. layoutSubviews , it can correctly adjust the width of the UITextField :

 @implementation AdaptingTextFieldToolbar - (void)layoutSubviews { CGFloat totalItemsWidth = 0.0; CGFloat itemsMargin = 8.0; UIBarButtonItem *textFieldBarButtonItem; for (UIBarButtonItem *barButtonItem in self.items) { // Get width of bar button item (hack from other SO question) UIView *view = [barButtonItem valueForKey:@"view"]; if(view) { if([view isKindOfClass:[UITextField class]]) { textFieldBarButtonItem = barButtonItem; } else if(view.bounds.size.width > 0) { // Docs say width can be negative for variable size items totalItemsWidth += view.bounds.size.width + itemsMargin; } } totalItemsWidth += itemsMargin; } textFieldBarButtonItem.width = (self.bounds.size.width - totalItemsWidth) - itemsMargin; NSLog(@"WIDTH=%f", textFieldBarButtonItem.width); [super layoutSubviews]; } @end 
+3
source

All Articles