Search code examples
iosuinavigationcontrolleruitoolbar

What is the correct approach for a custom UIToolBar?


Is it better - better in the sense of not getting rejected from the Apple store - when trying to create a custom* UIToolbar to either:

Option A

  • Add the oversize middle button as a UIButton to the self.navigationController.view this is key as I'm adding a UIButton to the navigationController which seems to contradict the apple docs
  • Use UIToolbar appearance to implement the custom background

Option B

  • Create a UIToolbar and add it as a subview of the current ViewController in UINavigationController
  • Add the UIButton as a subview of the current ViewController in UINavigationController
  • Use UIToolBar's method setBackgroundImage to add the custom background

*Custom background, Oversized middle button

This is the shape of the toolbar:

enter image description here

Extra details: This app is for iOS 5.X or greater. It uses UINavigationController. Key challenge is that a section of the app hide/shows the navigationbar depending on the state of the app.

Option A means I don't have to workout where to place the UIToolBar, it's a challenge to work out where to place the UIToolbar because a) if I'm showing or hiding the navigationBar this shifts everything up/down vertically b) iPhone 5 with extra vertical space - I can't use autolayout as I'm support iOS 5 and I haven't really worked out how to use autoresizing masks. I'mu using hard coded "magic" numbers.


Solution

  • I wouldn't do either of those.

    UIToolbar is nice when you want to do what it does, and it does allow some basic customization. But in this case you want a toolbar-like view that draws itself in a way that UIToolbar doesn't seem to allow for. You probably know exactly what items you want to appear in your "toolbar", and it's unlikely that you'll need to handle arbitrary assortments of UIBarItems.

    So, instead of giving yourself a headache trying to force UIToolbar to do something that it was never designed for, just create a simple view containing your background image (part of which will be transparent). Place some image-based UIButtons in the view, and call it a day. It wouldn't be a bad idea to create a custom UIView subclass representing your toolbar so that all the setup is nicely packaged up in one place, and so that you can reuse the toolbar in multiple views if you need to.