Search code examples
iosswift3uiviewuiviewcontrollerxib

Show a view on every view controller


I have to show a view on the current view controller that is being displayed on recieving a push notification. I want that i make one view that can add up as plugin to my current view controller, so that despite of adding the same view to every view controller. i just add to it the currently displayed view controller.

please help me with this.

Also, the view contains some buttons - so where should i add the button actions.


Solution

  • You can write an extension of UIViewController. Something like this.

    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
        }
    }