user2624177
user2624177

Reputation: 81

Changing UIToolbar Items Causes Toolbar to Go Blank

I have a custom UIToolbar created in Storyboard containing rewind, pause, and fast-forward UIBarButtonItems. I am attempting to replace the pause button with a play button when clicked. My code is as follows:

@IBOutlet weak var bottomToolbar: UIToolbar!

@IBAction func playPause() {
    var newButton: UIBarButtonItem
    if !self.timer.valid {
        let aSelector : Selector = "updateTime"
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
        newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause")
    }
    else {
        self.timer.invalidate()
        newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause")
    }
    var items = self.bottomToolbar.items
    items?[3] = newButton
    self.bottomToolbar.setItems(items, animated: true)
}

The toolbar is initialized with a dark gray background with white buttons. However, after the attempted button switch, the entire toolbar goes white. Any ideas?

UPDATE 1:
So after messing around with the colors and stepping through the code a bit more, I've found that only the pause/play button disappears in addition to the toolbar going white. But even after disappearing, clicking where it should be still sends a signal to the VC. And trying to reset the toolbar background to dark gray doesn't help.

Upvotes: 4

Views: 710

Answers (1)

bhr
bhr

Reputation: 2337

I had a similar problem recently. What helped was setting toolbar.Items to an empty array before setting it again to your items.

My swift is very limited, so please excuse any compiler errors:

 items = self.bottomToolbar.items
 self.bottomToolbar.setItems:([], animated: false)
 items?[3] = newButton
 self.bottomToolbar.setItems(items, animated: true)

Upvotes: 2

Related Questions