UIBarButtonItem更改播放/暂停按钮的图像

时间:2015-08-10 16:45:08

标签: ios image swift uibarbuttonitem

我正在尝试制作播放/暂停按钮,以便在按下时自定义图像之间切换。这是代码片段。

一切正常,但按钮不会改变。我也尝试过使用.image属性而不是.setBackGroundImage方法,但是点击后按钮就会变成空白。

这是代码

import UIKit
import AVFoundation

class ViewController: UIViewController {

var audioPlayer = AVAudioPlayer()

var pauseImage:UIImage?
var playImage:UIImage?

@IBOutlet weak var btnPlayPause: UIBarButtonItem!

@IBOutlet weak var sldVolume: UISlider!

@IBAction func sldVolumeChanged(sender: AnyObject) {

    audioPlayer.volume = sldVolume.value

}

@IBAction func actPressedPlayPause(sender: AnyObject) {

    if audioPlayer.playing {
        audioPlayer.pause()
        btnPlayPause.setBackgroundImage(playImage, forState: .Normal, barMetrics: .Default)

    } else {
        audioPlayer.play()
        btnPlayPause.setBackgroundImage(pauseImage, forState: .Normal, barMetrics: .Default)
    }

}

@IBAction func actStopPressed(sender: AnyObject) {

    audioPlayer.stop()
    audioPlayer.currentTime = 0

}

override func viewDidLoad() {
    super.viewDidLoad()

    var pauseImagePath = NSBundle.mainBundle().pathForResource("pause", ofType: "png")
    pauseImagePath = pauseImagePath!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    pauseImage = UIImage(contentsOfFile: pauseImagePath!)

    var playImagePath = NSBundle.mainBundle().pathForResource("play", ofType: "png")
    playImagePath = playImagePath!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    playImage = UIImage(contentsOfFile: pauseImagePath!)



    var audioPath = NSBundle.mainBundle().pathForResource("minuetcminor", ofType: "mp3")

    audioPath = audioPath!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

    var error : NSError? = nil

    audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath!), error: &error)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

4 个答案:

答案 0 :(得分:16)

在这里有同样的问题!如果我们两个人混淆为什么它不起作用,我认为这是Swift 2 / Xcode 7 beta的变化

这样做:

btnPlayPause.image = UIImage(named: "myImage.png")

希望有所帮助!

答案 1 :(得分:0)

尝试使用以下代码行:

make-polar

这适合我。

答案 2 :(得分:0)

@IBAction应使用sender作为UIButton,而不是AnyObjectAny。此外,完成后,可以使用sender从函数内部直接访问该按钮。 (这是Swift 3)

@IBAction func actPressedPlayPause(sender: UIButton) {

    if audioPlayer.playing {
        audioPlayer.pause()
        sender.setBackgroundImage(UIImage(named: "playimage.png"), for: .normal)

    } else {
        audioPlayer.play()
        sender.setBackgroundImage(UIImage(named: "pauseimage.png"), for: .normal)
    }

}

答案 3 :(得分:0)

似乎这个问题有很多奇怪的答案。事实上,在现有的UIBarButtonItem中没有非黑客的方式来改变图像。相反,不幸的是,你必须重新创建它。 我已经做了一个辅助方法来做到这一点:

func changeBarButtonItemImage(_ item: UIBarButtonItem, image: UIImage, navItem: UINavigationItem) -> UIBarButtonItem? {

    let buttonItem = UIBarButtonItem(image: image, style: item.style, target: item.target, action: item.action)
    buttonItem.isEnabled = item.isEnabled

    if let leftIndex = navItem.leftBarButtonItems?.index(of: item) {
        var items: [UIBarButtonItem] = navItem.leftBarButtonItems!
        items[leftIndex] = buttonItem
        navItem.leftBarButtonItems = items
        return buttonItem
    }

    if let rightIndex = navItem.rightBarButtonItems?.index(of: item) {
        var items: [UIBarButtonItem] = navItem.rightBarButtonItems!
        items[rightIndex] = buttonItem
        navItem.rightBarButtonItems = items
        return buttonItem
    }

    return nil
}

用法:

if let image = UIImage(named: showingFilter ? "icon_filter_active.png" : "icon_filter.png") {
    if let buttonItem = changeBarButtonItemImage(self.filterButton, image: image, navItem: navigationItem) {
        self.filterButton = buttonItem
    }
}

请注意我也复制了isEnabled属性,因为在我的情况下,在某些情况下,我在处于禁用状态时更改了按钮图标。