简体   繁体   中英

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.

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

It will be good if you present the ViewController on window to show push. On window we don't need to care about the currently visible controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM