Aligning UIToolBar items
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIToolbar in UIKit positions its UIBarButtonItem items in a horizontal row, and alignment is controlled by inserting special spacer items between buttons. A flexibleSpace item expands to fill available space (pushing items apart), while a fixedSpace item adds an exact pixel gap. By combining these spacers with your action items, you can left-align, center, or right-align buttons — or spread them evenly across the toolbar. Since iOS 14, you can also use UIBarButtonItem with SF Symbols and menu support.
Basic Toolbar Setup
Without spacers, items are packed together on the left side of the toolbar.
Right-Aligning Items
Place a flexibleSpace before the items to push them to the right:
This is the most common pattern — a Done button right-aligned on a toolbar above a keyboard.
Centering Items
Place flexible spaces on both sides:
The two flexible spaces expand equally, centering the title.
Evenly Distributed Items
Place flexible spaces between every item:
Each flexible space gets an equal share of the remaining width, distributing buttons evenly.
Fixed Spacing Between Items
Use fixedSpace for precise pixel gaps:
Toolbar Above Keyboard (Input Accessory View)
A common use case is adding a toolbar above the keyboard:
Using UINavigationController's Built-in Toolbar
UINavigationController has a built-in toolbar you can show without creating one manually:
Each view controller can provide its own toolbarItems, and they update automatically during navigation transitions.
Common Pitfalls
- Forgetting to set the toolbar frame for input accessory views: When used as
inputAccessoryView, the toolbar needs an explicit frame height (typically 44 points). Without it, the toolbar may have zero height and be invisible above the keyboard. - Creating a single spacer instance and reusing it in the items array: Each position in the
itemsarray needs its ownUIBarButtonIteminstance. Reusing the same flexible space object at multiple positions causes layout issues because UIKit tracks each item's position individually. - Not using
safeAreaLayoutGuidefor bottom toolbar placement: On devices with a home indicator (iPhone X and later), constraining the toolbar toview.bottomAnchorinstead ofview.safeAreaLayoutGuide.bottomAnchorcauses the toolbar to extend behind the home indicator, making bottom buttons hard to tap. - Setting
toolbar.itemsbefore adding the toolbar to the view hierarchy: While this usually works, some layout calculations may not apply correctly. Set items after the toolbar is part of the view hierarchy and constraints are activated. - Mixing
setItems(_:animated:)with directitemsassignment: UsesetItems(_:animated: true)for animated transitions between toolbar configurations. Directitemsassignment does not animate, which can look jarring when swapping button sets.
Summary
- Use
flexibleSpacebar button items to push toolbar items apart — one on the left for right-alignment, one on each side for centering - Use
fixedSpacewith a setwidthfor precise pixel gaps between items - Place flexible spaces between every item for even distribution
- For keyboard toolbars, create a
UIToolbarwith an explicit frame and assign it totextField.inputAccessoryView - Use
UINavigationController's built-inisToolbarHiddenandtoolbarItemsinstead of managing a separate toolbar when inside a navigation controller - Each spacer item must be a separate instance — do not reuse the same
UIBarButtonItemobject at multiple positions
Related reading
- All com.android.support libraries must use the exact same version specification
- Allow multi-line in EditText view in Android?
- Allow only Numbers for UITextField input
- Allow user to select camera or gallery for image
- Allowing interaction with a UIView under another UIView
- Always pass weak reference of self into block in ARC?
- An Android app remembers its data after uninstall and reinstall
- An App ID with Identifier '' is not available. Please enter a different string
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.