我在 View Controller 中将 FBAdView 添加到 UIView。 (我将 View 背景设置为红色) FBAdView 是使用 kFBAdSizeHeight50Banner 的 adSize 创建的。 问题是 FBAdView 在添加时会计算其宽度,但在旋转设备后它不会再次计算其宽度
我试过使用自动布局,但没有用
添加 FBAdview 的代码(添加到带有红色背景的 UILabel)
FBAdView *fbAdView = [[FBAdView alloc] initWithPlacementID:@"***************_***************"
adSize:kFBAdSizeHeight50Banner
rootViewController:self];
fbAdView.delegate = self;
[fbAdView loadAd];
[self.banner addSubview:fbAdView];
自动布局代码 - 不起作用
<p>// Width constraint, parent view width
[self.banner addConstraint:[NSLayoutConstraint constraintWithItem:fbAdView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.banner
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];</p>
<pre><code>// Height constraint, parent view height
[self.banner addConstraint:[NSLayoutConstraint constraintWithItem:fbAdView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.banner
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
// Center horizontally
[self.banner addConstraint:[NSLayoutConstraint constraintWithItem:fbAdView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.banner
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self.banner addConstraint:[NSLayoutConstraint constraintWithItem:fbAdView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.banner
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
</code></pre>
打印屏幕 添加横幅后,它适合屏幕宽度 旋转后它不会改变它的宽度(就像它的父红色 View 一样)
最佳答案
我认为您遗漏了一些限制条件。
添加这些行(可能需要一些小的改动)让你的代码工作:
self.banner.translatesAutoresizingMaskIntoConstraints = NO;
fbAdView.translatesAutoresizingMaskIntoConstraints = NO;
// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.banner
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
// Height constraint
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[banner(==50)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(banner)]];
我在 https://github.com/overlordnyaldee/BannerAutoLayout 发布了一个示例项目
关于ios - Facebook Audience Network ios FBAdView 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424997/