How to view remove subview?

To do what you ask you could have the overlay view as an optional, class level property, and try to remove it when the view appears

class VC1: UIViewController {
  var overlayView: UIView?


override func viewDidAppear() {
  super.viewDidAppear()
  overlayView?.removeFromSuperView()
}

Or you could use a protocol / delegate pattern to inform VC1 that VC2 was bring removed and use that method to remove the overlayView. This is a cleaner solution.

However if you’re doing whatever I think you are tyring to do (see my comment) I think there is a better approach – handle it all in the second view controller. Make the main view of second view controller do the masking by setting it’s alpha, then add a container view to the centre of that view and add all your content/functionality into that container view. This way when you dismiss the second the blur layer will go with it.

Leave a Comment