加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iOS:在屏幕上添加具有修复位置的子视图

发布时间:2020-12-15 01:50:14 所属栏目:百科 来源:网络整理
导读:如何在屏幕上修复子视图位置(尤其是在UIScrollView和UITableView中)?我想在故事板中 [self.view addSubview:aSubView]; 不再起作用了. 有任何想法吗? 编辑#1:我使用的是UITableViewController,而不是简单的UITableView. 编辑#2: CGRect fixedFrame = sel
如何在屏幕上修复子视图位置(尤其是在UIScrollView和UITableView中)?我想在故事板中

[self.view addSubview:aSubView];

不再起作用了.

有任何想法吗?

编辑#1:我使用的是UITableViewController,而不是简单的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,这会更容易一些,但不管怎么说也不是那么难.

UITableView是UIScrollView的子类,因此表视图的委托(在本例中为您的UITableViewController实例)也将接收UIScrollViewDelegate方法调用.您所要做的就是实现每次滚动偏移更改时调用的方法,并调整“固定”视图的框架.

像这样的东西:

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

将表格视图顶部所需的点数替换为20.您仍然将self.fixedView添加为self.view的子视图,这将确保它看起来像是在表视图上方的固定位置.

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

- (void)viewDidLoad {
    menuViewRelative = [[UIView alloc] init];
    menuViewRelative.backgroundColor = [UIColor grayColor];
    menuViewRelative.frame = CGRectMake(0.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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读