我在“测试广告客户”iAd Banner上 clic 时遇到了一些布局问题:
我不明白,因为一切都很好显示在横幅上之前。
我正在使用自动布局。
这是我的视图层次结构 的UIView | 内容查看 |导航栏 |泰伯维 | BannerView
我保留了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];
}