0

On my toolbar I am trying to properly lay out three buttons. One in the middle that should remain centered horizontally, and then one aligned left and one aligned right. But because the text for the left button is longer than either the middle button or the right one, the spacer items won't seem to produce the desired spacing effect. I've tried both fixed and flexible, but of course using the fixed type will not dynamically respond to different screen widths, and the the flexible width ones (UIBarButtonSystemItemFlexibleSpace) will not keep the middle button centered.

I've defined the layout both in Interface Builder and programmatically and I get the same effect, what you see here. For example an iPhone 6s looks like:

enter image description here

and an iPhone 11 Pro looks like:

enter image description here

As you can see the "Mid" button is not centered. By using UIBarButtonSystemItemFixedSpace instead of UIBarButtonSystemItemFlexibleSpace I could define the width of the spacers exactly, but in order to calculate it properly I would need to know the width of each UIBarButtonItem and also the right delegate method to do this in. Is this even possible, or must I do a custom view (or something else) to make this UIToolbar look like I want?

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
  • 1
    Maybe [this answer](https://stackoverflow.com/a/46581264/1987726) helps? – Reinhard Männer Apr 16 '20 at 12:41
  • Reinhard, thx. Yes, I read that before I posted, since it is linked to my question in the sidebar at right. For that reason I acknowledged in my question that I might have to use a custom view, but am also asking for an alternative. – Alyoshak Apr 21 '20 at 16:03

1 Answers1

0

Try adding leading spaces to the Shorter title OR using the following items:

Longer-Text Button, Flexible Space, Mid Button, Flexible Space, Fixed Space, Shorter Button

The fixed space would be a hard coded value to match the width delta between the two buttons.

Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
  • Brody, yes, I could do that, but would have to do it for each and every type of device, right? I alluded to this in my question. But let me know if I'm wrong and missing something here in your answer. – Alyoshak Apr 21 '20 at 16:01
  • I'm not sure I understand your comment. You could identify a way to calculate the difference in width between your left and right buttons to determine the size of the additional fixed space item. – Brody Robertson Apr 23 '20 at 15:24
  • But how to obtain the width value you speak of, both in your question and your comment? Can you be specific as to how? – Alyoshak Apr 24 '20 at 02:52
  • There's no way I can find to do that. – Alyoshak May 22 '20 at 22:45