iOS:在屏幕上添加具有修复位置的子视图 [英] iOS: Add subview with a fix position on screen

查看:156
本文介绍了iOS:在屏幕上添加具有修复位置的子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在屏幕上修复子视图位置(尤其是在UIScrollView和UITableView中)?我认为在故事板中

How do I fix a subviews position on screen (especially in UIScrollView and UITableView)? I think in storyboard

[self.view addSubview:aSubView];

不再有效。

任何想法?

编辑#1 :我使用的是UITableViewController,而不是简单的UITableView。

EDIT #1: I am using a UITableViewController, not a simple UITableView.

编辑#2

CGRect fixedFrame = self.menuViewRelative.frame;
fixedFrame.origin.y = 0 + scrollView.contentOffset.y;
self.menuViewRelative.frame = fixedFrame;

menuViewRelative = [[UIView alloc] init];
menuViewRelative.backgroundColor = [UIColor grayColor];
menuViewRelative.frame = CGRectMake(0.0, 0.0, 320.0, 50.0);

[self.view addSubview:self.menuViewRelative];


推荐答案

正如其他人所说,如果这样做会更容易一点你没有使用 UITableViewController ,但它并不是那么难。

As others noted, this would be a bit easier if you didn't use a UITableViewController, but it's not that hard anyway.

UITableView UIScrollView 的子类,所以表视图的委托(在这种情况下你的 UITableViewController 实例) )还将收到 UIScrollViewDelegate 方法调用。您所要做的就是实现每次滚动偏移更改时调用的方法并调整固定视图的框架。

UITableView is a subclass of UIScrollView, so table view's delegate (your UITableViewController instance in this case) will also receive UIScrollViewDelegate method calls. All you have to do is implement the method that gets called every time scroll offset changes and adjust the frame of your "fixed" view.

这样的事情:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect fixedFrame = self.fixedView.frame;
    fixedFrame.origin.y = 20 + scrollView.contentOffset.y;
    self.fixedView.frame = fixedFrame;
}

将表格顶部的20分替换为20分视图。您仍然添加 self.fixedView 作为 self.view 的子视图,这只是确保它看起来像是在在桌面视图上方的固定位置。

Replace 20 by how many points you want it to be from top of the table view. You still add self.fixedView as a subliew of self.view, this will just make sure it looks like it's in a fixed position above table view.

编辑:,您发布的代码,我'猜测你的版本应该是这样的:

with the code you posted, I'm guessing your verion should look like this:

- (void)viewDidLoad {
    menuViewRelative = [[UIView alloc] init];
    menuViewRelative.backgroundColor = [UIColor grayColor];
    menuViewRelative.frame = CGRectMake(0.0, 0.0, 320.0, 50.0);

    [self.view addSubview:self.menuViewRelative];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
    CGRect fixedFrame = self.menuViewRelative.frame;
    fixedFrame.origin.y = 0 + scrollView.contentOffset.y;
    self.menuViewRelative.frame = fixedFrame;
}

这篇关于iOS:在屏幕上添加具有修复位置的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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