【发布时间】:2020-02-27 13:58:40
【问题描述】:
我正在创建一个音乐播放器应用程序,并且我有一个播放/暂停栏来控制音乐。
我希望能够在不同的 VC:s 中跨我的应用程序使用此栏,但如果不重新启动每个 VC 中的“播放/暂停视图”,我无法弄清楚如何做到这一点。如果我重新启动它,暂停和播放按钮的状态不会通过 VC:s 进行通信。我目前在每个 VC 中创建一个容器视图,并将其分配给名为“PlayBar.swift”的播放/暂停栏类。
这是我当前的带有 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
}
}
【问题讨论】:
-
这是否类似于
UITabBar在您导航到其他视图时保持显示?
标签: ios swift uiview storyboard xib