gpt4 book ai didi

ios - 将 AdMob 横幅放置在安全区域的顶部 (iPhone X)

转载 作者:行者123 更新时间:2023-11-28 20:54:43 25 4
gpt4 key购买 nike

在 AdMob 中是否声明“通过修改所使用的属性和 anchor ,可以轻松地将这些技术用于限制到安全区域的顶部。”我不确定在这里要更改哪些值我知道它必须是属性和 anchor 但我不确定我应该将它们更改为什么我对约束不太熟悉

- (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 标准解决方案以向 View 添加横幅并设置所需的约束,以下代码段非常有用。

iOS 11 与之前版本的不同之处在于,11 引入了安全区域。 iOS 11 之前有 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).

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

关于ios - 将 AdMob 横幅放置在安全区域的顶部 (iPhone X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com