Facebook Audience Network ios FBAdView宽度

时间:2015-07-15 08:14:52

标签: ios objective-c facebook facebook-audience-network

我在视图控制器中向一个UIView添加了一个FBAdView。 (我将视图背景设置为红色) FBAdView是使用kFBAdSizeHeight50Banner的adSize创建的。 问题是FBAdView在添加时会计算其宽度,但在旋转设备后它不再计算其宽度

我尝试使用自动布局,但它无法正常工作

添加FBAdview的代码(使用红色背景添加到UILabel)

FBAdView *fbAdView = [[FBAdView alloc] initWithPlacementID:@"***************_***************" adSize:kFBAdSizeHeight50Banner rootViewController:self]; fbAdView.delegate = self; [fbAdView loadAd]; [self.banner addSubview:fbAdView];

autolayout的代码 - 不起作用

// Width constraint, parent view width [self.banner addConstraint:[NSLayoutConstraint constraintWithItem:fbAdView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.banner attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];

// 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]];

// 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]];

打印屏幕 添加横幅时,它适合屏幕宽度 旋转后它不会改变它的宽度(就像它的父红色视图)

The banner after rotating

1 个答案:

答案 0 :(得分:1)

我认为你错过了一些限制。

添加这些行(可能需要进行一些小的更改)会使您的代码工作:

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

发布了一个示例项目