UIButtonBarItem不变

时间:2018-12-30 00:02:33

标签: ios swift uibarbuttonitem

我在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!

1 个答案:

答案 0 :(得分:2)

您想将工具栏钩为插座

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