iOS:将UIView置于固定位置的UITableView之上 [英] iOS: Place UIView on top of UITableView in fixed position

查看:248
本文介绍了iOS:将UIView置于固定位置的UITableView之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的iphone应用程序中的UITableView上放置一个UIView(用于广告)。问题是,当我将表格滚动到底部时,添加的UIView将与表格一起滚动。我想要的是将它固定在屏幕的底部。有没有办法做到这一点?

I need to put a UIView (for ads) on top of a UITableView in my iphone app. The problem is that when I scroll the table to the bottom the added UIView is scrolling with the table. What I want is for it to be fixed on the bottom of the screen. Is there a way to do that?

这是我用来将UIView添加到表中的代码:

This is the code which I have used to add the UIView to the table:

 awView = [AdWhirlView requestAdWhirlViewWithDelegate:self]; 
 awView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
 [self.tableView addSubview:awView];


推荐答案

我很欣赏这是一个老问题。但我发现答案的部分内容是错误信息,而且片段不清楚。因此,对于它仍然值得的,我在这里是如何在我的 UITableViewController 的视图底部添加一个浮动视图。是的,你可以这样做,即使接受的答案说你做不到。

I appreciate this is an old question. But I've found the answers either with false information in part and unclear snippets. So for what it's still worth, here is how I added a "floating" view to the bottom of my UITableViewController's view. Yes, you can do that, even if the accepted answers says you cannot.

在你的 -viewDidLoad 方法中,你可以创建一个我们将其命名为bottomFloatingView的视图。这也是作为属性设置的。

In your -viewDidLoad method, you can create a view which we will name bottomFloatingView. This is also set up as a property.

请务必在表格视图的底部添加内容插件,这样可以避免将任何表格的内容隐藏起来浮动视图。

Be sure to add a content inset to the bottom of your table view, this will avoid hiding any of the table's content with your floating view.

接下来,您应该使用 UIScrollViewDelegate 来更新浮动视图的框架。

Next, you should use the UIScrollViewDelegate to update the frame of the floating view.

错觉将是你的观点陷入困境。实际上,此视图在您滚动时始终在移动,并且始终被计算为显示在底部。滚动视图非常强大!可能是我认为最被低估的UIKit课程之一。

The illusion will be that your view is stuck to the bottom. In reality, this view is moving all the time you are scrolling, and is always being computed to appear at the bottom. Scroll views are very powerful ! And probably are one of the most underrated UIKit classes I think.

所以这是我的代码。请注意该属性,表视图上的内容插入以及 -scrollViewDidScroll:委托方法实现。我在我的故事板中创建了我的浮动视图,这就是为什么你看不到正在设置的原因。

So here is my code. Note the property, the content inset on the table view and the -scrollViewDidScroll: delegate method implementation. I created my floating view in my storyboard which is why you can't see that being setup.

另外不要忘记你应该也可以使用KVO来观察变化表格视图的框架。有可能随着时间的推移而改变,最简单的测试方法是在模拟器中打开和关闭呼叫状态栏。

Also don't forget you should probably also use KVO to observe changes to the table view's frame. It's possible for that to change over time, the easiest way to test that is by toggling on and off the in call status bar in the simulator.

最后一件事,如果你在表格视图中使用节标题视图,这些视图将是表格视图中的最顶层视图,因此您还需要将浮动视图置于前面,在更改其框架时执行此操作。

Last thing, if you're using section header views in your table view, those views will be the top most view in the table view so you'll also want to bring your floating view to the front, do this when you change its frame.

@interface MyTableViewController ()
@property (strong, nonatomic) IBOutlet UIView *bottomFloatingView;
@end

@implementation MyTableViewController

static NSString *const cellIdentifier = @"MyTableViewCell";

- (void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:@"frame"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView addSubview:self.bottomFloatingView];

    self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);

    [self.tableView addObserver:self
                     forKeyPath:@"frame"
                        options:0
                        context:NULL];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];

    return cell;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self adjustFloatingViewFrame];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        [self adjustFloatingViewFrame];
    }
}

- (void)adjustFloatingViewFrame
{
    CGRect newFrame = self.bottomFloatingView.frame;

    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(self.bottomFloatingView.bounds);

    self.bottomFloatingView.frame = newFrame;
    [self.tableView bringSubviewToFront:self.bottomFloatingView];
}

@end

这篇关于iOS:将UIView置于固定位置的UITableView之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆