iPhone 应用程序中的全局 ADBannerView

发布于 2024-09-18 10:20:01 字数 226 浏览 4 评论 0原文

使用标准的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末 2024-09-25 10:20:01

编辑:在 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 中执行了以下操作:

- (void)loadView
{
    [super loadView];

    _contentView = [self.view retain];

    self.view = [[[UIView alloc] initWithFrame: _contentView.frame] autorelease];

    [self.view addSubview: _contentView];

    _adView = [[ADBannerView alloc] initWithFrame: CGRectMake(0, _contentView.bounds.size.height, 320, 50)];
    _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    _adView.delegate = self;

    [self.view addSubview: _adView]; 

    /* for visual debugging of view layout
    [[_mainView layer] setCornerRadius: 6.0];
    [[_mainView layer] setMasksToBounds: YES];
    [[_mainView layer] setBorderWidth: 1.5];
    [[_mainView layer] setBorderColor: [[UIColor grayColor] CGColor]];  
     */
}

注意发生了什么 - 我让超类 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:

- (void)loadView
{
    [super loadView];

    _contentView = [self.view retain];

    self.view = [[[UIView alloc] initWithFrame: _contentView.frame] autorelease];

    [self.view addSubview: _contentView];

    _adView = [[ADBannerView alloc] initWithFrame: CGRectMake(0, _contentView.bounds.size.height, 320, 50)];
    _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    _adView.delegate = self;

    [self.view addSubview: _adView]; 

    /* for visual debugging of view layout
    [[_mainView layer] setCornerRadius: 6.0];
    [[_mainView layer] setMasksToBounds: YES];
    [[_mainView layer] setBorderWidth: 1.5];
    [[_mainView layer] setBorderColor: [[UIColor grayColor] CGColor]];  
     */
}

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!

独留℉清风醉 2024-09-25 10:20:01

在 Apple 的开发示例代码中,iAdSuite 项目内容项目已为您完成此操作。强烈推荐。

In Apple's dev sample code the iAdSuite project contents projects that have this done for you. Highly recommended.

白况 2024-09-25 10:20:01

在我的根视图控制器(w/ ADBannerViewDelegate)中,我通过将横幅添加到导航控制器视图来设置横幅,这使其始终保持在顶部:

banner = [[ADBannerView alloc] init];
banner.delegate = self;
banner.frame = CGRectMake(0.0, 430.0, banner.frame.size.width, banner.frame.size.height);
[self.navigationController.view addSubview:banner];

请注意,您必须在委托方法bannerViewDidLoadAd中注释掉layoutAnimated,因为它会尝试移动广告视图向上:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    //[self layoutAnimated:YES];
}

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:

banner = [[ADBannerView alloc] init];
banner.delegate = self;
banner.frame = CGRectMake(0.0, 430.0, banner.frame.size.width, banner.frame.size.height);
[self.navigationController.view addSubview:banner];

Note you will have to comment out layoutAnimated in delegate method bannerViewDidLoadAd as it will try to move the ad view up:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    //[self layoutAnimated:YES];
}
且行且努力 2024-09-25 10:20:01

我采用了此处给出的 iAdSuite 中建议的方法

http:// developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html

我下载了代码并重点关注“选项卡”示例。我将 BannerViewController.h/.m 按原样复制到我的项目中。

我使用故事板方法以通常的方式创建了所有视图。然而,在我的 AppDelegate 类中,我随后访问了已经构建的选项卡栏 - 包含所有故事板构建的视图控制器。

AppDelegate 类实现 TabBarControllerDelegate 协议:

@interfaceAppDelegate : UIResponder <UITabBarControllerDelegate, UIApplicationDelegate>

AppDelegate 实现 didFinishLaunchingWithOptions 方法获取预构建的 tabBar,将其委托设置为 self(例如 AppDelegate 类)。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
// ----------------------------------------------------------
// Set the TabBarController delegate to be 'self'
// ----------------------------------------------------------
_tabBarController = (UITabBarController*)self.window.rootViewController;

//  tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
_tabBarController.delegate = self;

// update tab bar per iAdSuite approach
[self updateiAd];

然后,我根据 iAdSuite 方法构建了一组新的控制器,并使用这些新的选项卡栏项目重置选项卡栏。

-(void)updateiAd {
NSArray* viewControllers = [_tabBarController viewControllers];
NSMutableArray*newViewControllers = [[NSMutableArray alloc] init];
BannerViewController*bvc=NULL;
for(UIViewController * vc in viewControllers) {
    bvc = [[BannerViewController alloc] initWithContentViewController:vc];
    [newViewControllers addObject:bvc];
}

// set the new view controllers, replacing the original set
[_tabBarController setViewControllers:newViewControllers];
}

这种方法完全按照需要将相同的“广告”放置在每个视图的底部。我还必须在每个自定义 viewController 的 viewDidLoad 方法中设置视图标题(不知何故,在栏项目上设置它似乎不起作用,也没有设置图像;但是后者可能反映了我的图像的问题)。

我的原始配置是

                           TabViewController

NavController1               NavController2          NavController3  ...
      |                            |                       |
CustomViewController1      CustomViewController2      CustomViewController3

现在我的最终配置是

                           TabViewController

NavController1               NavController2          NavController3  ...
     |                            |                       |
iAdView1                         iAdView2               iAdView3
    |                            |                       |
CustomViewController1      CustomViewController2      CustomViewController3

在视图生命周期方面,我应该补充一点,在调用 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:

@interfaceAppDelegate : UIResponder <UITabBarControllerDelegate, UIApplicationDelegate>

The AppDelegate implementation didFinishLaunchingWithOptions method grabs the pre-built tabBar, setting its delegate to self (e.g. the AppDelegate class).

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
// ----------------------------------------------------------
// Set the TabBarController delegate to be 'self'
// ----------------------------------------------------------
_tabBarController = (UITabBarController*)self.window.rootViewController;

//  tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
_tabBarController.delegate = self;

// update tab bar per iAdSuite approach
[self updateiAd];

I then built a new set of controllers per the iAdSuite approach and reset the tab bar with these new tab bar items.

-(void)updateiAd {
NSArray* viewControllers = [_tabBarController viewControllers];
NSMutableArray*newViewControllers = [[NSMutableArray alloc] init];
BannerViewController*bvc=NULL;
for(UIViewController * vc in viewControllers) {
    bvc = [[BannerViewController alloc] initWithContentViewController:vc];
    [newViewControllers addObject:bvc];
}

// set the new view controllers, replacing the original set
[_tabBarController setViewControllers:newViewControllers];
}

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

                           TabViewController

NavController1               NavController2          NavController3  ...
      |                            |                       |
CustomViewController1      CustomViewController2      CustomViewController3

My final configuration is now

                           TabViewController

NavController1               NavController2          NavController3  ...
     |                            |                       |
iAdView1                         iAdView2               iAdView3
    |                            |                       |
CustomViewController1      CustomViewController2      CustomViewController3

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文