I want to support a layout similar to Safari, where in horizontally regular environments, buttons are displayed on the navigation bar, but in horizontally compact environments, some buttons are on the navigation bar and some buttons are on the toolbar.
This is what I have in my traitCollectionDidChange
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
let editingContextButton = editing ? resetButton : doneButton
if traitCollection.horizontalSizeClass == .Regular {
navigationController?.toolbarHidden = true
navigationItem.leftBarButtonItems = [editButtonItem(), helpButton]
navigationItem.rightBarButtonItems = [editingContextButton, addButton]
} else if traitCollection.horizontalSizeClass == .Compact{
navigationController?. toolbarHidden = false
navigationItem.leftBarButtonItems = [editButtonItem()]
navigationItem.rightBarButtonItems = [editingContextButton]
navigationController?.toolbarItems = [helpButton, UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil), addButton]
}
}
But it seems there is a problem when transitioning from a regular horizontal size class to a compact one. The items in the navigation bar are always correct, but the toolbar items are cut off or empty.
When bringing up the ViewController, it is always set up and displayed correctly, and it's always right in the horizontally regular size class – there's only a problem when the size class changes from regular to compact.
Here's an example, showing the add button in the bottom right disappearing from the toolbar after the change in size class.
The same thing happens when rotating the device on 5.5" iPhones, and in portrait on iPad, the toolbar is just empty after activating multitasking. What's the problem here? Thanks!
I realized that I was not setting the toolbar items correctly. Instead of
navigationController?.toolbarItems = [helpButton, UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil), addButton]
I should have done:
setToolbarItems([helpButton, UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil), addButton], animated: false)