1

Basically, the title says everything. I'm trying to do some customization and i need to use less-than-zero width to make buttons be closer to each other.

I've learned it from this answer.

I can do it by manually setting every fixed space width, but how can I do it via [appearance] or something to make it default?

Something like this:

// works for every button not only Fixed Space
[UIBarButtonItem appearance] setWidth:-10]; 

!!! OR !!!

How can i set less-than-zero width in XCode? I'm creating everything using Storyboard anyway, it says that's not a valid value to set.

Any help would be really great.

Community
  • 1
  • 1
dreamzor
  • 5,795
  • 4
  • 41
  • 61

1 Answers1

2

I normally solve this kind of problem by subclassing the container and overriding layoutSubviews or didLayoutSubviews. This gives you a lot of control of all kind of styling behavior. In this particular case you would determine the widths programmatically and adjust the frames accordingly.

Make your instance class a subclass of this subclass, and you will have the layout code neatly separated from your implementation.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks! Can I avoid the subclassing? It just feels bad for me to subclass something that I use only in storyboard GUI for 1 line of code... :) – dreamzor Jan 07 '13 at 14:15
  • If you do not plan on reusing this, you can just override these methods in your original subclass. – Mundi Jan 07 '13 at 14:59
  • Well, the problem is that I actually need to reuse them about 10 or more times... Not in code, but in storyboard, on different screens. – dreamzor Jan 07 '13 at 15:13
  • Then you should make a separate subclass as the parent of all your instances. In storyboard, you can easily assign this class to your storyboard objects. – Mundi Jan 07 '13 at 16:10
  • Yeah, now doing this. Should i override the `initWithCoder` method to set width here? Thanks for helping! – dreamzor Jan 07 '13 at 16:51
  • 1
    I usually define my own `doInit` task and call that in `init`, `initWithCoder` and any other common initializers like `initWithTitle:etc`. – Mundi Jan 07 '13 at 17:04