1
votes

I'm trying to add a UISegmentedControl to a UIToolbar using IB within xCode.

Whilst this is easy using a xib file, (create a view, add a toolbar to the bottom of the screen and drag on a UISegmentedControl onto the toolbar), it seems 'impossible' using storyboards.

I have a UIViewController scene embedded in a UINavigationController...

enter image description here

I have the following settings on the NavigationController...

enter image description here

I seem to be able to add UIBarButtonItems to the toolbar in the ViewController, not a UISegmentedControl as I can with xibs.

enter image description here

It even seems impossible to do via code...

let segmentControl = UISegmentedControl(items: ["One","Two"]);
let segmentedControlButtonItem = UIBarButtonItem(customView: segmentControl);
let barArray = [segmentedControlButtonItem];
self.navigationController?.setToolbarItems(barArray, animated: false);

Surely this is doable via storyboards? I can add UISegmentControls to the top bar in the Navigation Controller, (via a UIView) but not to the bottom toolbar.

How can this be done?

1

1 Answers

0
votes

You need to set the toolbarItems of your own ViewController, this will propagate to the NavigationController:

self.toolbarItems = barArray

I haven't found a way of adding a SegmentedControl via Storyboards, which I would prefer as well.