在每个视图控制器上显示一个视图
Show a view on every view controller
我必须在收到推送通知时显示的当前视图控制器上显示一个视图。我希望我制作一个可以作为插件添加到我当前的视图控制器的视图,这样尽管向每个视图控制器添加了相同的视图。我只是将当前显示的视图控制器添加到其中。
请帮我解决这个问题。
此外,该视图包含一些按钮 - 那么我应该在哪里添加按钮操作。
您可以编写 UIViewController
的扩展。像这样。
extension UIViewController {
func showNotificationView() {
let notiView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
notiView.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
notiView.backgroundColor = .red
self.view.addSubview(notiView)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.addTarget(self, action: #selector(callbackButton(_:)), for: .touchUpInside)
button.backgroundColor = .green
notiView.addSubview(button)
}
@objc func callbackButton(_ sender:Any) {
// button callback
}
}
如果在window上出示ViewController表示推送就好了。在 window 我们不需要关心当前可见的控制器。
我必须在收到推送通知时显示的当前视图控制器上显示一个视图。我希望我制作一个可以作为插件添加到我当前的视图控制器的视图,这样尽管向每个视图控制器添加了相同的视图。我只是将当前显示的视图控制器添加到其中。
请帮我解决这个问题。
此外,该视图包含一些按钮 - 那么我应该在哪里添加按钮操作。
您可以编写 UIViewController
的扩展。像这样。
extension UIViewController {
func showNotificationView() {
let notiView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
notiView.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
notiView.backgroundColor = .red
self.view.addSubview(notiView)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.addTarget(self, action: #selector(callbackButton(_:)), for: .touchUpInside)
button.backgroundColor = .green
notiView.addSubview(button)
}
@objc func callbackButton(_ sender:Any) {
// button callback
}
}
如果在window上出示ViewController表示推送就好了。在 window 我们不需要关心当前可见的控制器。