Remove all subviews?

前端 未结 15 1695

When my app gets back to its root view controller, in the viewDidAppear: method I need to remove all subviews.

How can I do this?

15条回答
  •  遥遥无期
    2020-11-28 00:51

    For ios6 using autolayout I had to add a little bit of code to remove the constraints too.

    NSMutableArray * constraints_to_remove = [ @[] mutableCopy] ;
    for( NSLayoutConstraint * constraint in tagview.constraints) {
        if( [tagview.subviews containsObject:constraint.firstItem] ||
           [tagview.subviews containsObject:constraint.secondItem] ) {
            [constraints_to_remove addObject:constraint];
        }
    }
    [tagview removeConstraints:constraints_to_remove];
    
    [ [tagview subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    

    I'm sure theres a neater way to do this, but it worked for me. In my case I could not use a direct [tagview removeConstraints:tagview.constraints] as there were constraints set in XCode that were getting cleared.

提交回复
热议问题