remove subview of uiwindow?

19,680

Solution 1

You can remove the a single subview using the following code.

[subview_Name removeFromSuperview];

if you want to remove all subviews form the view then use this.

NSArray *subViewArray = [self.window subviews];
for (id obj in subViewArray)
{
    [obj removeFromSuperview];
}

Solution 2

Hope the below code will be useful for remove particular view

   Set tag for that remove view

   subview.tag = 1;

   then

   [[[self window] viewWithTag:1] removeFromSuperview];

Solution 3

Swift version of @Maddy 's answer:

//create view then add a tag to it. The tag references the view
var myNewView = UIView()
myNewView.tag = 100

//add the  view you just created to the window
window.addSubview(myNewView)

//remove the view you just created from the window. Use the same tag reference
window.viewWithTag(100)?.removeFromSuperview

Update

Here is another way to remove a UIView without using a tag from the window. The key is the view has to be an instance property.

lazy var myNewView: UIView = {
    let view = UIView()
    return view
}()

viewDidLoad() {

    guard let window = UIApplication.shared.windows.first(where: \.isKeyWindow) else { return }

    window.addsubView(myNewView)
}

// call this in deinit or wherever you want to remove myNewView
func removeViewFromWindow() {

    guard let window = UIApplication.shared.windows.first(where: \.isKeyWindow) else { return }

    if myNewView.isDescendant(of: window) {
        print("myNewView isDescendant of window")
    }

    for view in window.subviews as [UIView] where view == myNewView {
        view.removeFromSuperview()
        break
    }

    if myNewView.isDescendant(of: window) {
        print("myNewView isDescendant of window")
    } else {
        print("myNewView is REMOVED from window") // THIS WILL PRINT
    }
}

deinit() {
    removeViewFromWindow()
}
Share:
19,680
Ahamed Aathil
Author by

Ahamed Aathil

iOS application developer

Updated on June 15, 2022

Comments

  • Ahamed Aathil
    Ahamed Aathil almost 2 years

    i want to remove a view from uiwindow,so i nslog in appdelegate method,it says window's subviews count as two NSLog(@" %d",[[self.window subviews] count]); so how can i remove that subviews from window,if i remove that subviews i have tab bar controller to be continued...

    - (void) GetUserCompleted
    
    {
        NSLog(@"   %@",[[self.window subviews] objectAtIndex:0]);   
        NSLog(@"   %@",[[self.window subviews] objectAtIndex:1]); 
    }
    
  • Ahamed Aathil
    Ahamed Aathil about 11 years
    thank u vinu!!!!i got some idea!!!but it removes all object!!!i want to remove a particular uiview?how can i?
  • trojanfoe
    trojanfoe about 11 years
    And what happens to the view controllers associated with these views? This might remove the view, but I think the OP is misunderstanding something.
  • Shahbaz Akram
    Shahbaz Akram about 6 years
    No known class method for selector 'window'
  • Maddy
    Maddy about 6 years
    @ShahbazAkram you can [UIApplication sharedApplication].keyWindow in Objective C. For swift you can use UIApplication.shared.keyWindow keywindow This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible() message.
  • l.vasilev
    l.vasilev about 6 years
    Really interesting, this solution works for me, but I still don't understand why just calling myNewView. removeFromSuperview() doesn't work? Any ideas?
  • Lance Samaria
    Lance Samaria about 6 years
    I’m not sure either. I’ll look into when I get some time and get back to you. You can always post it as a new question too!
  • Andrew Koster
    Andrew Koster almost 4 years
    It's possible that when you call removeFromSuperview on a specific variable, there's a another copy of it on the window. So it's removing the view that you're asking it to, but leaving the other copy that looks the same, and that's why it looks like it's doing nothing.
  • Hourglasser
    Hourglasser over 3 years
    Thanks. Much better than iterating all subviews. Just make sure to choose safe tag. so tag collision has the smallest probability. I chose -15 for example :)
  • Lance Samaria
    Lance Samaria almost 3 years
    @l.vasilev hi, I just found a new way to remove a view from the UIWindow by using .removeFromSuperview(). I updated my answer with it.