1

当我点击“测试广告商”iAd 横幅时,我遇到了一些布局问题:

广告点击

点击后查看

我不明白,因为在我点击横幅之前,一切都很好地展示了。

点击前查看

我正在使用自动布局。

这是我的视图层次结构 UIView | 内容视图 |导航栏 |表格视图 |横幅视图

我保留了 contentView 底部约束和bannerView 底部约束的参考

这是我的代码:

- (id)init
{
    self = [super init];
    if (self)
    {
        _eventsController = [[EventsController alloc]init];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"showADs"])
        {
            NSLog(@"Show ADs");
            _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
            [self.bannerView setDelegate:self];
        }
    }
    return self;
}

ViewDidAppear

- (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];

    NSLog(@"viewDidAppear");
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"showADs"])
    {
        [self.view addSubview:self.bannerView];
        [self.bannerView setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView
                                                             attribute:NSLayoutAttributeLeading
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.contentView
                                                             attribute:NSLayoutAttributeLeading
                                                             multiplier:1.0 constant:0]];

        self.bannerviewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.bannerView
                                     attribute:NSLayoutAttributeBottom
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.contentView
                                     attribute:NSLayoutAttributeBottom
                                    multiplier:1.0 constant:50];
        [self.view addConstraint:self.bannerviewBottomConstraint];
        [self.view setNeedsUpdateConstraints];
        [self.view setNeedsLayout];
    }
}

AdBannerViewDelegate

#pragma mark - ADBannerView Delegate
- (void)updateLayoutForAD
{
    if (self.bannerView.bannerLoaded)
    {
        self.contentViewBottomConstraint.constant = -50.0;
        self.bannerviewBottomConstraint.constant = 0;
    }
    else
    {
        self.contentViewBottomConstraint.constant = 0;
        self.bannerviewBottomConstraint.constant = 50;
    }

    [self.view setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"GOT AD");    
    [self updateLayoutForAD];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"DIDN'T GOT AD");
    [self updateLayoutForAD];
}
4

0 回答 0