iOS:将UIView置于固定位置的UITableView之上

Moh*_*gab 27 uitableview uiview ios

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

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

 awView = [AdWhirlView requestAdWhirlViewWithDelegate:self]; 
 awView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
 [self.tableView addSubview:awView];
Run Code Online (Sandbox Code Playgroud)

Adr*_*cas 33

这是它对我有用的方式.广告停留在视图的底部.

在ViewDidLoad中,在YourController.m中:

awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
awView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-kAdWhirlViewHeight/2);
[self.view addSubview:awView];
Run Code Online (Sandbox Code Playgroud)

然后在同一个.m文件中的某处添加此方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect newFrame = awView.frame;
    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y+(self.tableView.frame.size.height-kAdWhirlViewHeight);
    awView.frame = newFrame;
}
Run Code Online (Sandbox Code Playgroud)

别忘了声明awView.

  • 它应该只是newFrame.origin.y = aScrollView.contentOffset.y (3认同)

Dan*_*iel 29

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

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

确保将内容插入添加到表视图的底部,这样可以避免使用浮动视图隐藏任何表的内容.

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

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

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

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

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

@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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ich 10

  1. 将您的视图添加到表视图的超级视图中(如果可能,UITableViewController使其无法实现).

  2. 将您的视图添加到表视图并在-scrollViewDidScroll:委托方法中重新定位(UITableViewDelegate是子协议UIScrollViewDelegate).

  • 不要使用self.view.bounds!仅将origin.y-value调整为self.tableView.contentOffset.y (2认同)

小智 2

对于像我这样正在寻找使用 Swift 的简单解决方案的人来说,这些答案有点过时了。这是我所做的(假设 myCustomView 是在文件中的其他位置建立的):

func scrollViewDidScroll(_ scrollView: UIScrollView) {
   
    let pixelsFromBottom = CGFloat(20)//or whatever the
    let theHeight = self.tableView.frame.height + scrollView.contentOffset.y
    myCustomView.frame = CGRect(x: 0, y: theHeight - pixelsFromBottom , width: self.view.frame.width, height: myCustomView.frame.height)

}
Run Code Online (Sandbox Code Playgroud)