2

I'm running across this issue which I can't seem to find the answer to online. Basically what I'm trying to do is programmatically create a UIToolbar with some UIBarButtonItems.

What I've done (as detailed below) is create the UIToolbar and then set the items of the UIToolbar to an array holding all the UIBarButtonItems I want.

Unfortunately, though the UIToolbar shows up, the UIBarButtonItems have yet to show themselves

Any suggestions or explanations as to why this is happening is much appreciated!

class DrawViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //create bar buttons
        let deleteBarButton = UIBarButtonItem(image: UIImage(named: "greyDelete"), style: .Plain, target: self, action: "deleteView:")
        let eraseBarButton = UIBarButtonItem(image: UIImage(named: "greyErase"), style: .Plain, target: self, action: "erase:")
        let resizeBarButton = UIBarButtonItem(image: UIImage(named: "greyResize"), style: .Plain, target: self, action: "resize:")
        let viewBarButton = UIBarButtonItem(image: UIImage(named: "greyView"), style: .Plain, target: self, action: "view:")
        let colorBarButton = UIBarButtonItem(image: UIImage(named: "greyColor"), style: .Plain, target: self, action: "color:")
        let drawBarButton = UIBarButtonItem(image: UIImage(named: "greyDraw"), style: .Plain, target: self, action: "draw:")
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)

        //set up toolbar
        let toolbarItems = [deleteBarButton, flexibleSpace, eraseBarButton, 
                            flexibleSpace, resizeBarButton, flexibleSpace, 
                            viewBarButton, flexibleSpace, colorBarButton, 
                            flexibleSpace, drawBarButton]
        let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
        toolbar.barTintColor = UIColor(patternImage: UIImage(named: "blueToolbar")!)
        toolbar.setItems(toolbarItems, animated: true)
        self.view.addSubview(toolbar)
    }

As you can see only the toolbar show up

And the image names (of the images used to create the UIBarButtons) are the same names as those in the Assets catalog

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
14wml
  • 4,048
  • 11
  • 49
  • 97

1 Answers1

2

I would imagine the problem is the curious way you're creating the UIToolbar. This line here seems deeply suspect:

let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))

Why make it 0.7 times the height of your view?

To fix this problem, create the toolbar either without the frame constructor or with CGRectZero for its frame. Then use sizeToFit() to make it the appropriate size.

A simpler alternative, if possible, is to use a UINavigationController and tell it to show its own toolbar. You can then fill that by setting your view controller's toolbarItems property to your array, and it will all work without you having to create, position or manage a UIToolbar at all.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • Wow now I can see all my buttons! Thank-you! But now the toolbar shows up at the top of the screen instead of the bottom. Would you know how I could place the toolbar at the bottom? – 14wml Dec 17 '15 at 00:52
  • Nevermind I've figured it out! But thank-you for your help! You saved me so much time! – 14wml Dec 17 '15 at 01:22
  • I'm glad to hear it worked. Could you mark my answer as correct so that others can benefit too? Thank you! – TwoStraws Dec 17 '15 at 01:30