Customer toolbar item and auto-layout

I have seen that a similar question has been asked in 2017 but no there hasn't been a satisfactory answer.


I am adding a bar button item with a custom view to the toolbar. How do I adjust the height of the item to the maximum supported size of the toolbar (taking portrait and landscape orientations on an iPhone into consideration)?

Replies

Could you post the link of 2017 answer ?

Is it this one ?

https://stackoverflow.com/questions/43073738/change-size-of-uibarbuttonitem-image-in-swift-3


What did not work ?


To get the size of navigationBar, just use

    let bounds = self.navigationController!.navigationBar.bounds


You can change the height of navigation bar:

https://stackoverflow.com/questions/40751366/how-can-i-change-height-of-navigation-bar-swift-3


So what is the height you want to take into account ?


Note: it is a customer toolbar, more than a customer I guess 😉

Hi,


no, I am refering to this link: https://forums.developer.apple.com/message/214071#214071


The issue is that on an iPhone the height of the toolbar changes depending on the orientation. I would like to add a custom toolbar item [[UIBarButtonItem alloc] initWithCustomView:myView] whose height is automatically adjusted when the toolbar's height changes. And I prefer to used auto-layout.


Currently, I assign a modify the view's constraint whenever the device is rotated (something like [[[myView heightAnchor] constraintEqualToConstant:xx]] setActive:YES]; xx depends on the orientation of the device). But this is everything but nice. I prefer a constraint relation with the height of the toolbar.


PS: The spell checker seems to have messed up the title of this topic :-(

So, if you use


let bounds = self.navigationController!.navigationBar.bounds


That will give you the height of the toolbar.


let height = bounds.height


Now you can adpat the frame of the item.

Hi,

yes, that is possible. But I would like to use auto-layout.

In autoLayout, I would try to set equal height betwwen the button and the container (nav bar).


But note, that in some cases, trying to put in auto layout by all means is much worse than writing a few lines of code.

Hi,


this is not possible because


a) UIBarButtonItem is not derived from UIView.

b) Using the custom view might be possible, but I do not know when the view is actually assigned to the toolbar.