I am creating a music player app and i have a play/pause bar to control the music.
I want to be able to use this bar across my app in different VC:s but can't figure out how to do this without reinitiating the "play/pause-view" in each VC. If i reinitiate it the status of the pause & play-button does not communicate across the VC:s. I currently create a container view in each VC and assign it to the Play/Pause-bar Class called "PlayBar.swift".
This is my current code for the play/pause-bar with a xib:
class PlayBar: UIView {
let kCONTENT_XIB_NAME = "PlayBarView"
@IBOutlet weak var playBtnOutlet: UIButton!
@IBOutlet weak var premiumBtnOutlet: UIButton!
@IBOutlet weak var timerBtnOutlet: UIButton!
@IBOutlet var playBarView: UIView!
static var currentWindow = UIViewController()
static var currentlyPlaying = false
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed(kCONTENT_XIB_NAME, owner: self, options: nil)
playBarView.fixInView(self)
}
@objc func playBtnAction(sender: UIButton){
}
}
extension UIView
{
func fixInView(_ container: UIView!) -> Void{
self.translatesAutoresizingMaskIntoConstraints = false;
self.frame = container.frame;
container.addSubview(self);
NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
}
}
If i reinitiate it the status of the pause & play-button does not communicate across the VC:s.
So communicate it. It isn’t going to happen by itself. Pass state info from one vc to the next or keep it in a common location where all vcs can access it.
I currently create a container view in each VC and assign it to the Play/Pause-bar Class called "PlayBar.swift".
Nothing wrong with that, but if your goal is to have just one player bar then your architecture is backwards. Have just one player vc with the player bar and have it contain all the other vcs.
I’ve given two opposite possibilities because it isn’t clear which your goal is.