swiftuiviewviewcontrollernotificationcenter

Exist way notify when custom UIView will be presented or removeFromSuperview()?


I am create custom UIView using this code:

lazy var temporary: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.backgroundColor = .white
        return view
        
    }()

Exist way use NotificationCenter notify when this view will be presented and removed from VC if I am use:

self.view.addSubview(temporary)

or

temporary.removeFromSuperview()

Solution

  • I'm not totally sure what you are asking. I think you're asking for a way to tell when your custom view is added as a subview of another view.

    The easiest way to do that is to make your view a custom subclass of UIView, and implement didMoveToSuperview() or willMove(toSuperview:). Those methods get called when your view is added as a child view of another view.

    If you really want to use the notification center you could have your custom view class broadcast a notification when it gets added to a superview.