首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >问题定位横幅在我的UITableView上

问题定位横幅在我的UITableView上
EN

Stack Overflow用户
提问于 2010-09-14 22:36:24
回答 2查看 2.4K关注 0票数 4

我希望能够在我的ADBannerView (它显示一个UITableView)之上显示一个UITableView。不幸的是,我的旗帜没有正确定位。它将出现在UITableView的正下方,然后当我滚动时,横幅将保留在我的UITableView的中间。

我希望横幅出现在UITabBar上方,当用户拖动.时,允许UITableView在横幅后面滚动。

代码语言:javascript
复制
-(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;
                     }];
}
EN

回答 2

Stack Overflow用户

发布于 2014-01-31 03:10:13

我想出了如何在不使用带有内部UITableViewController的自定义UIViewController的情况下对通常的UITableView这样做。诀窍是动态调整横幅视图的位置,使其始终位于可见区域的底部。

您只需将横幅视图添加为子视图,并相应地调整表内嵌:

代码语言:javascript
复制
- (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);
}

在滚动表时,需要根据内容偏移量调整横幅位置:

代码语言:javascript
复制
- (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;
}

我的例子是底部定位的横幅,但很容易更改计算,以便将其应用于放置在表顶部的横幅。

票数 2
EN

Stack Overflow用户

发布于 2010-09-14 22:46:12

您应该创建一个视图,将表视图和横幅广告作为单独的实体保存,而不是求助于上面的恶意操作。也就是说,创建一个新的UIView,它将被设置为视图控制器的视图属性。在这个视图中,把你的横幅广告放在屏幕外面(它的y来源应该是横幅高度的负值),而你的桌面视图的来源应该是0,0。成功加载横幅后,将横幅动画为原点0, 0,并将表视图的原点移动到0,bannerHeight。

这将给你在WWDC10视频中看到的效果,如果你还没有看的话,我建议你看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3713417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档