简体   繁体   English

无法从超级视图中删除视图

[英]Unable to remove view from superview

I am using banner code, to show the banner in the below example code 我正在使用横​​幅代码,在下面的示例代码中显示横幅

   HZBannerAdOptions *options = [[HZBannerAdOptions alloc] init];
        [HZBannerAd requestBannerWithOptions:options success:^(HZBannerAd *banner) {
            [viewController.view addSubview:banner];
        } failure:^(NSError *error) {
            NSLog(@"Error = %@",error);
        }];

However, when I want to hide/remove the banner, I use this; 但是,当我想隐藏/删除横幅时,可以使用它。

[self.currentBannerAd removeFromSuperview];
    self.currentBannerAd = nil;

But it is not working, the banner is still there, I have tried some variants such as 但它不起作用,横幅仍然存在,我尝试了一些变体,例如

[currentBannerAd setHidden:YES];

With no success, any ideas how to remove this banner from the view? 没有成功,有什么想法如何从视图中删除此横幅?

When you add the banner to the view, you have no reference to it, you have to assign it to a property like this: 当您将横幅添加到视图时,您没有对其的引用,您必须将其分配给以下属性:

HZBannerAdOptions *options = [[HZBannerAdOptions alloc] init];
[HZBannerAd requestBannerWithOptions:options success:^(HZBannerAd *banner) {
    self.currentBannerAd = banner;
    [viewController.view addSubview:self.currentBannerAd];
} failure:^(NSError *error) {
    NSLog(@"Error = %@",error);
}];

And then remove it using your own code, just add the layoutIfNeeded line: 然后使用您自己的代码将其删除,只需添加layoutIfNeeded行:

[self.currentBannerAd removeFromSuperview];
self.currentBannerAd = nil;
[viewController.view layoutIfNeeded];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM