What is the correct way to remove a subview from a view hierarchy and nuke it?

后端 未结 8 1441
走了就别回头了
走了就别回头了 2021-01-31 15:34

I have a parent UIView with a number of subviews. Periodically I need to remove a subview and completely remove it from the system. What is the correct way to do this? I tried t

相关标签:
8条回答
  • 2021-01-31 15:40

    That's the right general idea. those other UIViews that disappear, what's their relationship to this UIView? Are they subviews of this view? Are they dealloc'd in the dealloc method of the view you're removing?

    Are you sure your Tags are unique?

    Sujal

    0 讨论(0)
  • 2021-01-31 15:41
        override func viewWillAppear(_ animated: Bool)
        {
            super.viewWillAppear(animated)
    
            if let topController = UIApplication.topViewController() {
    
                if topController.isKind(of: ProviderHome.self)
                {
                    let arrOfSuview = self.view.subviews
    
                    if arrOfSuview.count > 1
                    {
                        print("Davender Arr of subviews : \(arrOfSuview)")
    
                        for i in 0..<arrOfSuview.count
                        {
                            let objSub = arrOfSuview[i]
    
                            if objSub.tag == 101
                            {
                              objSub.removeFromSuperview()
                            }
    
                        }
    
    
    
                    }
    
    
                    NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelPushNotification), name: NSNotification.Name(rawValue: "handelPush"), object: nil)
    
                    NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelLocalNotification), name: NSNotification.Name(rawValue: "handelLocal"), object: nil)
                }
            }
    
    
        }
    
    @objc func handelPushNotification(_ notification: NSNotification)  {
    
    
            let arrOfSuview = self.view.subviews
    
            if arrOfSuview.count > 1
            {
                print("Davender Arr of subviews : \(arrOfSuview)")
    
                for i in 0..<arrOfSuview.count
                {
                    let objSub = arrOfSuview[i]
    
                    if objSub.tag == 101
                    {
                        objSub.removeFromSuperview()
                    }
    
                }
    
            }
    
            if notification.userInfo != nil
            {
    
    
                let dict = notification.userInfo as! Dictionary<String, Any>
    
                let d = dict["data"] as! Dictionary<String, Any>
    
                let action = d["gcm.notification.label"] as! String
    
                print("current message id :- ", action)
    
                self.getNotificationId = action
    
                if getNotificationId != ""
                {
                   //call the api for getting Data
    
                    AppDelegate.sharedInstance().myCurrentnotificationId = getNotificationId
    
                    //working code
                    let storyboard = UIStoryboard(name: "Provider", bundle: nil)
                    let vc = storyboard.instantiateViewController(withIdentifier: "CommonPopUpsVC") as! CommonPopUpsVC
                    vc.modalPresentationStyle = .overFullScreen
                    vc.view.frame = self.view.frame
                    vc.view.tag = 101
                    self.view.addSubview(vc.view)
                    self.present(vc, animated: true, completion: nil)
    
                }
    
    
           }
        }
    
    0 讨论(0)
  • 2021-01-31 15:49

    Is it possible that cell.contentView has the same tag as the subview you want to remove? according to the documentation viewWithTag removes:

    The view in the receiver’s hierarchy that matches tag. The receiver is included in the search.

    If this is the case then you may be inadvertently removing cell.contentView from the cell. If n is zero and your cell's contentview has no tag set to it, it would default to 0 and cause that to happen.

    0 讨论(0)
  • 2021-01-31 15:57

    Swift 3.0:

    let viewToRemove = mySuperView.viewWithTag(myTag)
    viewToRemove?.removeFromSuperview()
    
    0 讨论(0)
  • 2021-01-31 15:58

    To remove all subviews from your view:

    for(UIView *subview in [view subviews]) {
       [subview removeFromSuperview];
    }
    

    If you want to remove some specific view only then:

    for(UIView *subview in [view subviews]) {
      if([subview isKindOfClass:[UIButton class]]) {
         [subview removeFromSuperview];
     } else {
         // Do nothing - not a UIButton or subclass instance
     }
    }
    

    You can also delete sub views by tag value:

    for(UIView *subview in [view subviews]) {
        if(subview.tag==/*your subview tag value here*/) {
            [subview removeFromSuperview];
    
        } else {
            // Do nothing - not a UIButton or subclass instance
        }
    }
    
    0 讨论(0)
  • 2021-01-31 15:58

    Swift 4: extend UIView

    extension UIView {
        public func removeAllSubviews() {
            for subview in self.subviews {
                subview.removeFromSuperview()
            }
        }
    }
    

    or

    extension UIView {
        public func removeAllSubviews() {
            self.subviews.forEach { $0.removeFromSuperview() }
        }
    }
    
    0 讨论(0)
提交回复
热议问题