将 AdMob 横幅放置在安全区域的顶部 (iPhone X)
Position AdMob banner at top of the safe area (iPhoneX)
在 AdMob 中它是否声明 "The techniques can easily be used for constraining to the top of the safe area by modifying the attributes and anchors used." 我不确定在这里要更改哪些值我知道它必须是属性和锚点但我不确定我应该如何更改它们以使我不那么熟悉有约束
- (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]];
}
由于 iPhoneX 附带 iOS 11.0 及更高版本,您只需修改 positionBannerViewFullWidthAtBottomOfSafeArea 函数即可。无需调整适用于 11 之前版本的 positionBannerViewFullWidthAtBottomOfView 函数中的约束。
改变
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
至
[guide.topAnchor constraintEqualToAnchor:bannerView.topAnchor]
这会将 AdMob 横幅的顶部固定在指南的顶部。
优雅解
要缩短 admob 标准解决方案以将横幅添加到视图并设置所需的约束,以下代码段非常有用。
iOS11与之前版本的区别在于11引入了安全区。在 11 iOS 之前有 LayoutMargins。我们向 return 我们添加了一个小功能,我们可以是安全区域指南或布局边距,并通过以下方式摆脱整个 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).
//Call the method to set the layout guide.
let guide = correctLayoutGuide //Swift
UILayoutGuide * guide = [self correctLayoutGuide]; //Objective-C
[NSLayoutConstraint activateConstraints:@[
[guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],
//[guide.topAnchor constraintEqualToAnchor:bannerView.topAnchor] // Banner at TOP
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor] // Banner at BOTTOM
]];
}
//This function returns safeAreaLayoutGuide for iOS 11 and above
//and layoutMarginsGuide for iOS < 11.
//Swift
var correctLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
} else {
return view.layoutMarginsGuide
}
}
//Objective-C
-(UILayoutGuide *) correctLayoutGuide {
if (@available(ios 11.0, *)) {
return [self.view safeAreaLayoutGuide];
} else {
return [self.view layoutMarginsGuide];
}
}
在 AdMob 中它是否声明 "The techniques can easily be used for constraining to the top of the safe area by modifying the attributes and anchors used." 我不确定在这里要更改哪些值我知道它必须是属性和锚点但我不确定我应该如何更改它们以使我不那么熟悉有约束
- (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]];
}
由于 iPhoneX 附带 iOS 11.0 及更高版本,您只需修改 positionBannerViewFullWidthAtBottomOfSafeArea 函数即可。无需调整适用于 11 之前版本的 positionBannerViewFullWidthAtBottomOfView 函数中的约束。
改变
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
至
[guide.topAnchor constraintEqualToAnchor:bannerView.topAnchor]
这会将 AdMob 横幅的顶部固定在指南的顶部。
优雅解
要缩短 admob 标准解决方案以将横幅添加到视图并设置所需的约束,以下代码段非常有用。
iOS11与之前版本的区别在于11引入了安全区。在 11 iOS 之前有 LayoutMargins。我们向 return 我们添加了一个小功能,我们可以是安全区域指南或布局边距,并通过以下方式摆脱整个 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).
//Call the method to set the layout guide.
let guide = correctLayoutGuide //Swift
UILayoutGuide * guide = [self correctLayoutGuide]; //Objective-C
[NSLayoutConstraint activateConstraints:@[
[guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],
//[guide.topAnchor constraintEqualToAnchor:bannerView.topAnchor] // Banner at TOP
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor] // Banner at BOTTOM
]];
}
//This function returns safeAreaLayoutGuide for iOS 11 and above
//and layoutMarginsGuide for iOS < 11.
//Swift
var correctLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
} else {
return view.layoutMarginsGuide
}
}
//Objective-C
-(UILayoutGuide *) correctLayoutGuide {
if (@available(ios 11.0, *)) {
return [self.view safeAreaLayoutGuide];
} else {
return [self.view layoutMarginsGuide];
}
}