How to change the bar button icon in toolbar in swift? - ios

I am trying to toggle between the play button and pause button in swift. I have a bar button item in the toolbar whose identifier is initially set to the play. I tried to search and found out the following piece of code but it does not work, looks like it works when the bar button is in the navigation bar
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "TheMethodThatTheButtonShouldCall"), animated: true)

You need to set items on UIToolbar to update toolbar items: call func setItems(_items: [AnyObject]?,animated animated: Bool) with your new items to update the toolbar's items
Source: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIToolbar_Class/index.html#//apple_ref/occ/instm/UIToolbar/setItems:animated:

Implemented your question with the code as below. works in Swift 2. Please note I connected the IBAction outlet to playBtn
import UIKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet var sliderVal: UISlider!
#IBOutlet var theToolbar: UIToolbar!
var mySound = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("ColdPlay", ofType: "mp3")
do {
mySound = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
}
catch{
print("error caught")
}
}
#IBAction func playBtn(sender: UIBarButtonItem) {
let theBarbuttonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseBtn:")
let arrayBarButtonItem = [theBarbuttonItem]
theToolbar.setItems(arrayBarButtonItem, animated: true)
mySound.play()
}
#IBAction func pauseBtn(sender: UIBarButtonItem) {
let theBarbuttonItemB = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playBtn:")
let arrayBarButtonItemB = [theBarbuttonItemB]
theToolbar.setItems(arrayBarButtonItemB, animated: true)
mySound.pause()
}
}

Related

How to change the default name of file to share in Swift?

everyone!
I'm new in Swift and I've run into a problem.
I have an app that show images and I want to share some. When I click "share icon" there is default name "JPEG image". How can I change it to something like "defaultImageName".
Image I want to share
Share using default share in iOS
And here's default name that I want to change "JPEG image"
I've tried something like
imageView.image = UIImage(named: "defaultImageName")
But it's not correct, cause imageView.image it's not that I want to change.
Here's my code. I think it should be in #objc func shareTapped(), but don't really know how to do it.
import UIKit
class DetailViewController: UIViewController {
#IBOutlet var imageView: UIImageView!
var selectedImage: String?
var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Picture \(pictures.firstIndex(of: selectedImage!)!+1) of \(pictures.count)"
navigationItem.largeTitleDisplayMode = .never //
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
if let imageToLoad = selectedImage {
imageView.image = UIImage(named: imageToLoad)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.hidesBarsOnTap = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.hidesBarsOnTap = false
}
#objc func shareTapped() {
guard let image = imageView.image?.jpegData(compressionQuality: 0.8) else {
print("No image found")
return
}
let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
}

How to toggle Play and Pause with a UIBarbuttonItem

Can anyone help me. I've search for my question and still can't figure out a solution, everything I found was not formatted in swift 3. Im trying to make a toggle Play and Pause out of a UIBarButtonItem. Whenever the play button is click I want it to play the file and at the same time change the UIBarButtonItem to pause and vise versa.
By the way I have embed in a navigation controller into my main view controller from the editor.
Here is my Code Snippet.
import UIKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet weak var pausePlayBtn: UIBarButtonItem!
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
prepareMusic()
}
func prepareMusic() {
let path = Bundle.main.path(forResource: "SomeFileName", ofType: "TypeOfFile")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer.prepareToPlay()
} catch let err as NSError {
print(err)
}
}
#IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
} else {
audioPlayer.stop()
}
}
}
I've tried the following.
First I started by creating play button and a pause button and adding them in my togglePausePlay func.
let playBtn = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
let pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
#IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
pausePlayBtn = playBtn
} else {
audioPlayer.stop()
pausePlayBtn = pauseBtn
}
}
The second thing I tried was this.
#IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
} else {
audioPlayer.stop()
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
}
}
This work at the start of the app, I would click the button for the first time and everything would work the button would change to pause and file would start. However when I tried to click it again nothing happens, button stays on pause and file still keeps playing until I stop the project.
Any help would be appreciated. :)
i figured out a solution for u and its working for me.
Here's the code i tried and its working. the buttonitem is changing on its click.
import UIKit
class ViewController: UIViewController {
var playBtn = UIBarButtonItem()
var pauseBtn = UIBarButtonItem()
#IBOutlet var labelMusicState: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playBtn = UIBarButtonItem(barButtonSystemItem: .play , target: self, action: #selector(playBtnAction(sender:)))
self.pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause , target: self, action: #selector(pauseBtnAction(sender:)))
self.navigationItem.rightBarButtonItem = playBtn
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playBtnAction(sender: UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = pauseBtn
self.labelMusicState.text = "Music is Playing"
}
func pauseBtnAction(sender: UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = playBtn
self.labelMusicState.text = "Music Not Playing"
}
}
In both the scenarios, you are trying to assign .play button to your existing button whenever the player starts playing music. It should be opposite. whenever the player starts playing music, your button should change to .pause mode and when the player stops playing music, the button should go to .play mode.
do the following modification:
let playBtn = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
let pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
#IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
pausePlayBtn = pauseBtn
} else {
audioPlayer.stop()
pausePlayBtn = playBtn
}
}
You were doing the opposite due to which once the music starts playing, the button does not change its state from play to pause but remains in play state due to which the next time user presses the button, it plays the music and doesnt stop it.

Change bar button Item from Play to Pause Swift

I'm creating a Pomodoro Timer. And I want change the Play bar button item to Pause when I press on it. I've already created the IBOutlet for this button and IBAction.
#IBOutlet weak var playButton: UIBarButtonItem!
#IBAction func startTimer(sender: AnyObject) {
self.playButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: nil)
}
But this doesn't work. Please help.
try this,
func play() {
var pauseButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "pause") //Use a selector
navigationItem.rightBarButtonItem = pauseButton
//other stuff
}
func pause() {
var playButton = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "play") //Use a selector
navigationItem.rightBarButtonItem = playButton
//other stuff
}

UIBarButtonItem changing image for Play/Pause button

I'm trying to make a play/pause button that toggles between custom images as it is pressed. Here is a snippet of the code.
Everything works fine but the button doesn't change. I've also tried using the .image property instead of the .setBackGroundImage method but then the button just goes blank on click.
Here is the code
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.
}
}
had the same problem here! Had two of us confused as to why it wasn't working, I think it's a change in Swift 2/Xcode 7 beta
Just do this:
btnPlayPause.image = UIImage(named: "myImage.png")
Hope that helps!
Seems like this question has got a lot of strange answers. The truth is there's no non-hacky way to change an image in already existing UIBarButtonItem. Instead, unfortunately, you have to re-create it.
I've made a helper method to do this:
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
}
Usage:
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
}
}
Take note that I also copy isEnabled property, because, in my case, in some situations, I was changing button icon while in the disabled state.
The #IBAction should use sender as UIButton instead of AnyObject or Any. Also, once that is done, the button can be directly accessed from inside the function using sender. (This is 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)
}
}
Try using below line of code:
btnPlayPause.setImage(image, forState: .Normal)
this works for me.

Swift: change Bar Button Item in code

I am using swift. I have a Bar Button Item that I would like to change the Identifier from Play to Stop in code. Is this possible and how do you do It?
#IBOutlet var StartStopButton: UIBarButtonItem!
#IBAction func StartAlarm(sender: AnyObject) {
onOffIndicator.hidden = false
StartStopButton.Identifier = ?????
}
Unfortunately you can't change the identifier so you have to set the whole bar button item. You have to do the following:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: "startAlarm:")
To make it nicer you can define an array of UIBarButtonSystemItems and an index like so:
let myArray = [UIBarButtonSystemItem.Start, UIBarButtonSystemItem.Stop]
var index = 0
Then you can do:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: myArray[++index % myArray.count], target: self, action: "startAlarm:")
By the way, remember to use non-capitalized function and variable names ;)
Swift 5
#IBOutlet var StartStopButton: UIBarButtonItem!
#IBAction func StartAlarm(sender: AnyObject) {
onOffIndicator = !onOffIndicator
if onOffIndicator {
StartStopButton.image = UIImage(systemName: "stop")
} else {
StartStopButton.image = UIImage(systemName: "play")
}
}

Resources