0

我在视图控制器的 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];

自动布局代码 - 不起作用

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

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

旋转后的横幅

4

1 回答 1

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发布了一个示例项目

于 2015-07-16T04:45:36.273 回答