iPhone 应用程序中的全局 ADBannerView
使用标准的 UINavigationController 根应用程序是否可以在屏幕底部、视图层次结构下方显示单个 ADBannerView?也就是说,在不修改每个可以推送到根 UINavigationController 的视图控制器/视图的情况下,我可以让全局 ADBannerView 可见吗?
我不确定如何在 IB 或代码中进行设置。帮助?
我看到类似的问题,但答案很模糊。我正在寻找一个具体的例子。
Is it possible with a standard UINavigationController-rooted app, to have a single ADBannerView visible at the bottom of the screen, below the view hierarchy? That is, without modifying each view-controller/view that can be pushed to the root UINavigationController, can I have a global ADBannerView be visible?
I'm not sure how to set this up, either in IB or in code. Help?
I see similar questions with vague answers. I'm looking for a concrete example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:在 iOS5+ 中执行此操作的更好方法可能是使用视图控制器包含。也就是说,创建一个包含广告和应用程序控制器(导航、选项卡等)的根控制器。
我找到了一种方法来做到这一点。这就是我所做的:
在我的第一次尝试中,我创建了一个名为 AdBannerController 的新视图控制器。对于它的视图,我创建了一个全屏视图和两个子视图。第一个子视图 (contentView) 用于常规内容,第二个子视图是 AdBannerView。我使用此视图控制器的实例作为与应用程序窗口关联的视图控制器( [window addSubview: adBannerController.view] )。然后我将 UINavigationController.view 添加为 adBannerController.view 的子视图:[adBannerController.contentView addSubview:navigationController.view]。
除了推送到 UINavigationController 的视图控制器从未调用其 will/did-load/unload 方法之外,这大部分都有效。嘘。我在一些地方读到,这是 UINavigationController 视图不是应用程序窗口的直接后代的症状。
在我的第二次尝试中,我采用了相同的 AdBannerController 并从 UINavigationController 派生它。这次,我在 loadView 中执行了以下操作:
注意发生了什么 - 我让超类 UINavigationController 构造其常规“内容”视图,但我将其交换出来并用我自己的视图替换,该视图是内容视图和广告视图的容器。
这效果非常好。我也在使用 Three20,需要做一些事情才能使其与该设置一起工作,但还不错。
我希望这对某人有帮助!
EDIT: The better way to do this in iOS5+ is likely to use view controller containment. That is, make a root controller that contains your ad and application controller (nav, tab, etc.).
I figured out a way to do this. Here is what I did:
For my first attempt I created a new view controller called AdBannerController. For its view I created a full-screen view and two subviews. The first subview (contentView) is for regular content, the second is the AdBannerView. I used an instance of this view controller as the view controller associated with the app window ( [window addSubview: adBannerController.view] ). Then I added my UINavigationController.view as a subview of adBannerController.view: [adBannerController.contentView addSubview: navigationController.view].
This mostly worked except that viewcontrollers pushed to the UINavigationController never got their will/did-load/unload methods called. Shucks. I read in a few places that this is a symptom of the UINavigationController view not being a direct descendant of the app window.
For my second attempt I took the same AdBannerController and derived it from UINavigationController. This time, I did the following in loadView:
Notice what happens - I let the superclass UINavigationController construct its regular "content" view, but I swap it out and replace it with my own view which is a container for both the content and ad views.
This works pretty well. I'm also using three20 and there were a few things required to make this work with that setup, but not too bad.
I hope this helps someone!
在 Apple 的开发示例代码中,iAdSuite 项目内容项目已为您完成此操作。强烈推荐。
In Apple's dev sample code the iAdSuite project contents projects that have this done for you. Highly recommended.
在我的根视图控制器(w/ ADBannerViewDelegate)中,我通过将横幅添加到导航控制器视图来设置横幅,这使其始终保持在顶部:
请注意,您必须在委托方法bannerViewDidLoadAd中注释掉layoutAnimated,因为它会尝试移动广告视图向上:
In my root view controller (w/ ADBannerViewDelegate) I setup my banner by adding it to the nav controller view, which keeps it on top at all times:
Note you will have to comment out layoutAnimated in delegate method bannerViewDidLoadAd as it will try to move the ad view up:
我采用了此处给出的 iAdSuite 中建议的方法
http:// developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html
我下载了代码并重点关注“选项卡”示例。我将 BannerViewController.h/.m 按原样复制到我的项目中。
我使用故事板方法以通常的方式创建了所有视图。然而,在我的 AppDelegate 类中,我随后访问了已经构建的选项卡栏 - 包含所有故事板构建的视图控制器。
AppDelegate 类实现 TabBarControllerDelegate 协议:
AppDelegate 实现 didFinishLaunchingWithOptions 方法获取预构建的 tabBar,将其委托设置为 self(例如 AppDelegate 类)。
然后,我根据 iAdSuite 方法构建了一组新的控制器,并使用这些新的选项卡栏项目重置选项卡栏。
这种方法完全按照需要将相同的“广告”放置在每个视图的底部。我还必须在每个自定义 viewController 的 viewDidLoad 方法中设置视图标题(不知何故,在栏项目上设置它似乎不起作用,也没有设置图像;但是后者可能反映了我的图像的问题)。
我的原始配置是
现在我的最终配置是
在视图生命周期方面,我应该补充一点,在调用 updateiAd 方法时只有 NavController 存在。
调用完成后将创建单独的 CustomViewControllers1/2/3/etc。
I adapted the approach suggested in the iAdSuite given here
http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html
I downloaded the code and focused on the 'tab' example. I copied over the BannerViewController.h/.m as is into my project.
I created all my views in the usual way with the storyboard approach. However, in my AppDelegate class I then accessed the already built tab bar - containing all the storyboard built viewControllers.
The AppDelegate class implements the TabBarControllerDelegate protocol:
The AppDelegate implementation didFinishLaunchingWithOptions method grabs the pre-built tabBar, setting its delegate to self (e.g. the AppDelegate class).
I then built a new set of controllers per the iAdSuite approach and reset the tab bar with these new tab bar items.
This approach puts the same 'ad' at the bottom of each view, exactly as needed. I also had to set the view title in the viewDidLoad method of each custom viewController (somehow, setting it on the bar item didn't seem to work not did setting the image; the later may reflect an issue with my images however).
My original configuration was
My final configuration is now
In terms of view lifecycle, I should add that only the NavControllers are in existence at the time the updateiAd method is called.
The individual CustomViewControllers1/2/3/etc get created after the call completes.