要将AdMob关闭按钮放置在iPhone 12的安全区域上方,可以使用以下代码示例:
Objective-C:
// 获取安全区域Insets
UIEdgeInsets safeAreaInsets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;
// 创建关闭按钮
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setTitle:@"关闭广告" forState:UIControlStateNormal];
[closeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(closeAd) forControlEvents:UIControlEventTouchUpInside];
// 设置关闭按钮的位置和大小
CGFloat buttonHeight = 50.0;
CGFloat buttonWidth = 100.0;
closeButton.frame = CGRectMake(safeAreaInsets.left, safeAreaInsets.top, buttonWidth, buttonHeight);
// 将关闭按钮添加到广告视图中
[self.view addSubview:closeButton];
Swift:
// 获取安全区域Insets
if let safeAreaInsets = UIApplication.shared.windows.first?.safeAreaInsets {
// 创建关闭按钮
let closeButton = UIButton(type: .custom)
closeButton.setTitle("关闭广告", for: .normal)
closeButton.setTitleColor(.black, for: .normal)
closeButton.addTarget(self, action: #selector(closeAd), for: .touchUpInside)
// 设置关闭按钮的位置和大小
let buttonHeight: CGFloat = 50.0
let buttonWidth: CGFloat = 100.0
closeButton.frame = CGRect(x: safeAreaInsets.left, y: safeAreaInsets.top, width: buttonWidth, height: buttonHeight)
// 将关闭按钮添加到广告视图中
self.view.addSubview(closeButton)
}
这段代码首先获取了iPhone 12的安全区域Insets,然后创建了一个UIButton作为关闭按钮,设置了按钮的位置和大小,最后将按钮添加到广告视图中。你可以根据需要调整按钮的样式和位置。
上一篇:AdMob更新后无法加载广告