• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - GADBannerView 没有设置高度

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:09:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

有人知道如何以编程方式显示 50pt 高度的 GADBannerView 吗?

在我看来DidLoad,

int heightOfBanner = 50;
GADBannerView *banner = 
 [[GADBannerView alloc]
 initWithAdSize:GADAdSizeFullWidthPortraitWithHeight(heightOfBanner)
 origin:CGPointMake(0, CGSizeFromGADAdSize(kGADAdSizeBanner).height)];
banner.adUnitID = @"ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxx";
banner.rootViewController = self;
[self.view addSubview:banner];

[banner loadRequest:[GADRequest request]];

以上代码不显示任何内容。

但是当 heightOfBanner 设置为更高的值(例如 200)时,效果很好(但横幅的高度不是 50)。

我的 podfile 如下:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'Google-Mobile-Ads-SDK', '~> 7.0'

我成功完成了“pod install”。



Best Answer-推荐答案


不要使用精确的尺寸来绘制您的 GADBannerView。您应该将其框架和原点设置为相对于屏幕尺寸。这将在屏幕底部显示您的 GADBannerView

    // Get device screen size
    // For example, screenBounds on an iPhone 6 will look like this
    // screenBounds.origin.x == 0
    // screenBounds.origin.y == 0
    // screenBounds.size.width == 375
    // screenBounds.size.height == 667
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    // Setup AdMob view
    // Create the GADBannerView
    adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Use your BANNER_UNIT_ID
    adMobView.adUnitID = BANNER_UNIT_ID;
    adMobView.rootViewController = self;
    [adMobView loadRequest:[GADRequest request]];

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // screenBounds.size.width and adMobView.bounds.size.height sets the size of the GADBannerView
    [adMobView setFrame:CGRectMake(0, 0, screenBounds.size.width, adMobView.bounds.size.height)];

    // This will take the center of our GADBannerView and move it to a point (x,y)
    // We want our GADBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our GADBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our GADBannerView here so if we just set it to that
    // Half of our GADBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our GADBannerView's height to fix that with adMobView.bounds.size.height / 2
    adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (adMobView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:adMobView];

关于ios - GADBannerView 没有设置高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002758/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap