How to create fixed space and flexible space bar button items programmatically?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In UIKit, fixed-space and flexible-space UIBarButtonItem objects are mainly used in toolbars to control horizontal spacing between real buttons. A fixed space reserves an exact width, while a flexible space expands to consume any extra room left in the toolbar.
Fixed Space vs Flexible Space
These two system items solve different layout problems:
- '
fixedSpacecreates a known gap, measured in points' - '
flexibleSpacegrows or shrinks as the toolbar width changes'
That distinction matters because a fixed space is about precision, while a flexible space is about distribution. If the layout needs to adapt across iPhone sizes and rotations, flexible spacing is usually the better default.
Create a Fixed Space Item
A fixed space item becomes useful only after you assign its width.
Without a width, the item exists but does not communicate any meaningful spacing intent.
Create a Flexible Space Item
A flexible space item does not need a width because UIKit calculates it automatically.
You usually place this between real buttons so the available room gets pushed into the gap.
Build a Toolbar Item Array
The most common place to use these items is a UIToolbar.
In that layout, the flexible space pushes the save and delete buttons apart, while the fixed space preserves a small constant gap at the end.
Full Programmatic Example
Here is a minimal view controller that creates a toolbar entirely in code:
This is the programmatic equivalent of configuring toolbar spacing in Interface Builder.
Toolbars and Navigation Bars Are Not the Same
A common source of confusion is trying to use these spacing items in a navigation bar the same way they behave in a toolbar. Their behavior is most predictable in UIToolbar item arrays. Navigation bars often need custom views, layout margins, or appearance configuration instead of toolbar-style spacing items.
So the answer depends not only on the API call, but also on where the item will be used.
Common Pitfalls
Creating a fixed-space item without assigning its width leaves you with no intentional spacing, even though the code appears complete.
Expecting flexible space to create a specific pixel gap is incorrect because it expands dynamically to consume remaining toolbar width.
Using too many fixed spaces can make a toolbar look brittle across device sizes because the layout stops adapting naturally.
Trying to apply toolbar spacing assumptions directly to navigation bars often produces confusing results because the two layout systems behave differently.
Adding spacing items when a custom view or stack-based layout would be clearer can overcomplicate the interface.
Summary
- Create fixed-space and flexible-space items with
UIBarButtonItemsystem items. - Set
widthon a fixed-space item to control an exact gap. - Use flexible space when the layout should adapt to available width.
- These items are most predictable inside
UIToolbarconfigurations. - Choose fixed or flexible spacing based on whether the design needs precision or adaptability.

