Align UIKit Toolbar Items with Flexible Space
UIKit toolbars arrange UIBarButtonItem objects rather than accepting coordinates for every button. A flexible-space item consumes the available gap. Put one before an action to move the action toward the trailing side in a left-to-right layout, or put distinct flexible-space items on both sides to center it.
This avoids calculating offsets from a screen width that can change with rotation, window sizing or device configuration. It also keeps the action a normal bar button with UIKit interaction behavior. The flexible-space item's own presentation properties are ignored. Apple's flexible-space definition
Use the navigation controller's toolbar
This example belongs in a .NET 10 iOS application that uses UIKit. Install ToolbarDemoController inside the application's existing UINavigationController. The controller supplies toolbar items, and the navigation controller owns the visible toolbar.
using System;
using UIKit;
/// <summary>
/// Displays a centered toolbar action using separately owned spacer items.
/// </summary>
public sealed class ToolbarDemoController : UIViewController {
private UIBarButtonItem? LeadingSpace { get; set; }
private UIBarButtonItem? ActionItem { get; set; }
private UIBarButtonItem? TrailingSpace { get; set; }
public override void ViewDidLoad() {
base.ViewDidLoad();
View!.BackgroundColor = UIColor.SystemBackground;
Title = "Toolbar example";
LeadingSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
TrailingSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
ActionItem = new UIBarButtonItem("Apply", UIBarButtonItemStyle.Plain, HandleApply);
ToolbarItems = new[] { LeadingSpace, ActionItem, TrailingSpace };
}
public override void ViewWillAppear(bool animated) {
base.ViewWillAppear(animated);
NavigationController?.SetToolbarHidden(false, animated);
}
protected override void Dispose(bool disposing) {
if (disposing) {
ToolbarItems = Array.Empty<UIBarButtonItem>();
LeadingSpace?.Dispose();
ActionItem?.Dispose();
TrailingSpace?.Dispose();
LeadingSpace = null;
ActionItem = null;
TrailingSpace = null;
}
base.Dispose(disposing);
}
private void HandleApply(object? sender, EventArgs args) => Title = "Applied";
}
The constructors used above are available in the current .NET bindings. The normal action receives a managed event handler, while the spacers need no action. UIBarButtonItem constructors
Check the result and choose an arrangement
With this controller visible, the navigation toolbar should contain a centered Apply action. Tapping it changes the navigation title to Applied. Rotate the device or resize a supported window and confirm that the action remains centered.
For a single trailing action, assign ToolbarItems = new[] { LeadingSpace, ActionItem }; after creating those objects. For a single leading action, use the action followed by one flexible spacer. In a right-to-left language, check the actual directional behavior on the target system rather than assuming that trailing always means the physical right edge.
Each position has its own spacer object. Do not put the same UIBarButtonItem instance into the collection twice. For several actions, decide whether you want equal gaps or a grouped arrangement before inserting more spacers. A fixed-space item is appropriate for an intentional constant gap, not for absorbing the remaining toolbar width.
Keep toolbar ownership explicit
Setting ToolbarItems alone does not create a navigation controller or reveal a hidden toolbar. The example therefore requests visibility when it appears. Other controllers in the same navigation stack should apply their own toolbar policy when they appear.
ToolbarItems
,
SetToolbarHidden
The sample creates and owns these items. It clears the collection before disposing them when the controller is explicitly disposed. Do not dispose borrowed items still displayed by another owner, and do not assume that navigating away always disposes a controller. UIKit calls and explicit UI-object cleanup belong on the main thread.
This updates Herman Schoenfeld's toolbar-alignment answer , recorded under CC BY-SA 3.0 . The controller example is newly authored. Its binding signatures were checked, but visual behavior still needs an iOS device or simulator run, including accessibility text sizes and localized labels.
Author: Herman Schoenfeld