Search code examples
iosobjective-cuiviewcontrolleruitoolbar

Is it possible to place a UIToolbar below a UICollectionView without a UINavigationController?


I have a UICollectionViewController that contains a UICollectionView, and a UIToolbar. I would like to place the UIToolbar below the UICollectionView (at the bottom of the screen).

What I have now is the UICollectionView that goes all the way to the bottom of the screen and the UIToolbar placed on top of it in the zorder. In the picture below the collection view extends below the toolbar.

Toolbar on top of a CollectionViewController

I understand that I can do this with a NavigationController, but I have also read that navigation controllers are for drill down behavior. I don't want that- the items in this toolbar do not necessarily change the view. Also it seems like the NavigationController would make my code more complicated than necessary.

Note, my UIToolbar is added programmatically. I have also tried adding it in the storyboard but I can't figure out how to place the toolbar in my view. This is my storyboard:

enter image description here

I cannot drag the toolbar into the view and when I run this the storyboard toolbar does not appear.

This question/answer makes it seem like the toolbar can be "snapped" to the correct position or the scrollview can be "re-sized", but I'm not seeing this behavior.


Solution

  • If you have created a UICollectionViewController then the UICollectionView is the main view and as such you can't put things above of below it and that is why you can't drag the UIToolbar onto the view.

    What you will need to do is create a UIViewController which conforms to the UICollectionViewDataSource and UICollectionViewDelegate protocols instead. That will have a UIView as its main view and to that you can add a UICollectionView and a UIToolbar and position them as you want. Don't forget to also link the data source and delegate of the UICollectionView to your UIViewController.