UIButtonBarItem 没有改变

UIButtonBarItem not changing

我在 iOS 应用程序中有一个底部工具栏,它只包含一个用于播放/暂停直播 Icecast 广播流的按钮。一切正常,除了按钮从播放图标变为暂停图标,反之亦然。这是我的代码。

@IBAction func PlayStop(_ sender: Any) {
        if player.timeControlStatus == .playing {
            // Stop the live radio stream
            player.pause()
            // Set the radio tower image to off with Alpha 0.3
            TowerImage.image = UIImage(named: "TowerOff")
            TowerImage.alpha = 0.3
            // Set the Toolbar icon to the Play Icon
            PlayStopButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.play, target: self, action: #selector(ViewController.PlayStop(_:)))
        } else if player.timeControlStatus != .playing {
            // Play the live radio stream
            player.play()
            // Set the radio tower image to on with Alpha 1
            TowerImage.image = UIImage(named: "TowerOn")
            TowerImage.alpha = 1
            // Set the Toolbar icon to the Pause Icon
            PlayStopButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.pause, target: self, action: #selector(ViewController.PlayStop(_:)))
        }
    }
    @IBOutlet var TowerImage: UIImageView!
    @IBOutlet var PlayStopButton: UIBarButtonItem!

你想挂接工具栏作为插座然后

let playStopButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.pause, target: self, action: #selector(ViewController.PlayStop(_:)))  
self.toolBar.items = [playStopButton]