4

UITabBar (UITableView を表示する) の真上に ADBannerView を表示できるようにしたいと考えています。残念ながら、バナーが正しく配置されていません。UITableView のすぐ下に表示され、スクロールするとすぐにバナーが UITableView の中央に残ります。

バナーを UITabBar のすぐ上に表示し、ユーザーがドラッグしたときに UITableView がバナーの後ろにスクロールできるようにします。

-(void)layoutForCurrentOrientation:(BOOL)animated
{
    CGFloat animationDuration = animated ? 0.2 : 0.0;
    // by default content consumes the entire view area
    CGRect contentFrame = self.view.bounds;
    // the banner still needs to be adjusted further, but this is a reasonable starting point
    // the y value will need to be adjusted by half the banner height to get the final position
    CGPoint bannerCenter = CGPointMake(CGRectGetMidX(contentFrame), CGRectGetMaxY(contentFrame));
    CGFloat bannerHeight = 0.0;

    // First, setup the banner's content size and adjustment based on the current orientation
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
        bannerHeight = 32.0;
    }
    else
    {
        banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
        bannerHeight = 50.0;
    }

    // Depending on if the banner has been loaded, we adjust the content frame and banner location
    // to accomodate the ad being on or off screen.
    // This layout is for an ad at the bottom of the view.
    if(banner.bannerLoaded)
    {
        contentFrame.size.height -= bannerHeight;
        bannerCenter.y -= bannerHeight / 2.0;
    }
    else
    {
        bannerCenter.y += bannerHeight / 2.0;
    }

    // And finally animate the changes, running layout for the content view if required.
    [UIView animateWithDuration:animationDuration
                     animations:^{
                         self.tableView.frame = contentFrame;
                         [self.tableView layoutIfNeeded];

                         banner.center = bannerCenter;
                     }];
}
4

2 に答える 2

2

内部でUITableViewControllerカスタムを使用せずUIViewControllerに通常の方法でそれを行う方法を見つけました。UITableView秘訣は、バナー ビューの位置を動的に調整して、常に表示領域の下部に配置することです。

バナー ビューをサブビューとして追加し、それに応じてテーブルのインセットを調整しました。

- (void)viewDidLoad 
{
        [super viewDidLoad];

        ...

        //init banner and set it's frame (here we use 320x50 banner) 
        self.bannerView = [[UIView alloc] init];
        self.bannerView.frame = CGRectMake(0, self.view.frame.size.height - 50, 320, 50);

        //add as subview
        [self.view addSubview:self.bannerView];

        //set proper bottom inset depending on your banner height
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}

テーブルがスクロールされるとき、コンテンツ オフセットに応じてバナーの位置を調整する必要があります。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //refresh banner frame during the scrolling
    CGRect bannerFrame = self.bannerView.frame;
    bannerFrame.origin.y = self.view.frame.size.height - 50 + self.tableView.contentOffset.y;
    self.bannerView.frame = bannerFrame;
}

私の例は下部に配置されたバナーですが、テーブルの上部に配置されたバナーに適用するように計算を変更するのは簡単です。

于 2014-01-31T03:10:13.593 に答える
0

上記のハッカーに頼るのではなく、テーブルビューとバナー広告を別々のエンティティとして保持するビューを作成する必要があります。つまり、View Controller の「view」プロパティに設定される新しい UIView を作成します。このビューでは、バナー広告を画面のすぐ外に配置し (Y 原点はバナーの高さの負の値である必要があります)、テーブルビューの原点は 0,0 にする必要があります。バナーが正常に読み込まれたら、バナーを起点 0,0 にアニメートし、テーブルビューの起点を 0,bannerHeight に移動します。

これにより、iAds に関する WWDC10 ビデオで見られる効果が得られます。まだ視聴していない場合は、視聴することをお勧めします。

于 2010-09-14T22:46:12.987 に答える