指出:“可以通过修改所使用的属性和锚点,轻松地将这些技术用于约束安全区域的顶部。”我不确定要在此处更改哪些值,我知道它必须是属性和锚点,但是我不确定如何将其更改为对约束不熟悉
- (void)addBannerViewToView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
if (@available(ios 11.0, *)) {
// In iOS 11, we need to constrain the view to the safe area.
[self positionBannerViewFullWidthAtBottomOfSafeArea:bannerView];
} else {
// In lower iOS versions, safe area is not available so we use
// bottom layout guide and view edges.
[self positionBannerViewFullWidthAtBottomOfView:bannerView];
}
}
#pragma mark - view positioning
- (void)positionBannerViewFullWidthAtBottomOfSafeArea:(UIView *_Nonnull)bannerView NS_AVAILABLE_IOS(11.0) {
// Position the banner. Stick it to the bottom of the Safe Area.
// Make it constrained to the edges of the safe area.
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
]];
}
- (void)positionBannerViewFullWidthAtBottomOfView:(UIView *_Nonnull)bannerView {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
}
答案 0 :(得分:1)
由于iPhoneX随iOS 11.0及更高版本一起提供,您只需要修改 positionBannerViewFullWidthAtBottomOfSafeArea 函数。无需调整 positionBannerViewFullWidthAtBottomOfView 函数中的约束,该约束适用于11版之前的版本。
更改
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
到
[guide.topAnchor constraintEqualToAnchor:bannerView.topAnchor]
这会将AdMob横幅的顶部锚定在指南的顶部。
优雅的解决方案
要缩短admob标准解决方案以在视图中添加横幅并设置所需的约束,以下代码段非常有用。
iOS 11与以前的版本之间的区别是11引入了安全区域。在11之前,iOS拥有LayoutMargins。我们添加了一个小函数来返回“安全区域指南”或布局边距,并通过以下方法摆脱整个 positionBannerViewFullWidthAtBottomOfView 函数:
- (void)addBannerViewToView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[self positionBannerViewFullWidthAtBottomOfSafeAreaOrLayoutMargins:bannerView];
}
#pragma mark - view positioning
- (void)positionBannerViewFullWidthAtBottomOfSafeAreaOrLayoutMargins:(UIView *_Nonnull)bannerView {
// Position the banner. Stick it to the bottom of the Safe Area or layout margins.
// Make it constrained to the edges of the safe area or layout margins (iOS < 11).
let guide = correctLayoutGuide
[NSLayoutConstraint activateConstraints:@[
[guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
]];
}
//Sorry this is Swift code - I don't use ObjC.
//This function returns safeAreaLayoutGuide for iOS 11 and above
//and layoutMarginsGuide for iOS < 11.
var correctLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
} else {
return view.layoutMarginsGuide
}
}