Kurt Nobel
Kurt Nobel

Reputation: 5

GADBannerView removeFromSuperview not working

I am using GADBannerView to show banner ads. This is how I load ads:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        if !pro {
            // In this case, we instantiate the banner with desired ad size.
            bannerView = GADBannerView(adSize: kGADAdSizeBanner)

            addBannerViewToView(bannerView)

            bannerView.adUnitID = myAdUnit
            bannerView.rootViewController = self
            bannerView.load(GADRequest())

            bannerView.delegate = self

            print("did load banner")
        } else {
            if bannerView != nil {
                bannerView.isHidden = true
                bannerView.removeFromSuperview()
                print("Removed bannerView")
            }
        }
    }

So the loading works fine. My second view controller is connected in a tab bar controller. When I press the unlockButton in the second view controller it sets value pro to true. When navigating back to the first view controller, I can see it says Removed bannerView in the log view, but the banner is still there. Any tips?

Upvotes: 0

Views: 174

Answers (1)

Erik Auranaune
Erik Auranaune

Reputation: 1414

If you want to get rid of all the subview of the GADBannerView class, you can simply do:

for v in view.subviews {
    if v is GADBannerView {
        v.removeFromSuperview()
    }
}

Upvotes: 0

Related Questions