在 UIScrollView iOS 原点上方添加视图 [英] Adding view above origin of UIScrollView iOS

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

问题描述

我正在尝试在其原点上方添加 UIView.

I am trying to add UIViews above its origin.

例如

假设我在原点添加了子视图 A (x= 0 , y = 0);现在我想在这个子视图上方添加另一个子视图 B 所以我试过

Suppose i have added subview A at origin (x= 0 , y = 0); now i want to add another subview B above this subview so i tried

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -500, 200, 500)];
view.backgroundColor = [UIColor redColor];
[scrollView addSubview:view];

[scrollView setContentOffset:CGPointMake(0, 500)];
[scrollView setContentSize:CGSizeMake(300, scrollView.frame.size.height+500)];

我想在滚动到顶部时添加许多这样的视图,即当滚动视图到达顶部时在当前视图上方添加另一个视图,并且滚动应该没有任何缺失.

I want to add many views like this while scrolling to top i.e. when scrollview reaches at top add another view above current view and scrolling should be without any lacking.

但是使用上面的代码,它现在确实显示在添加的视图之上.

But with above code it does now show above added views.

推荐答案

如果我的理解是正确的,那么您的看法是错误的.如果您有一个总内容大小为 1000 的滚动视图,并且您将内容偏移设置为 500,那么如果您想添加一个位于滚动视图可见顶部上方 500 处的视图,您应该设置该视图原点为 0.尽管您希望此视图出现在滚动视图当前可见部分上方 500 pts 处,但您必须在滚动视图的坐标空间中创建坐标,该坐标从 0,0 开始,从左到右,从上到底部.试试这个:

If I'm understanding this correctly, you're looking at this the wrong way. If you have a scroll view with a total content size of lets say 1000, and you set the content offset to 500, then if you want to add a view that is 500 above the visible top of the scroll view, you should set that views origin to 0. Although you want this view to appear 500 pts above the currently visible portion of the scroll view, you have to create the coordinates in the coordinate space of the scroll view, which start at 0,0 left to right, top to bottom. Try this instead:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 500)];

由于要将此视图移动到现有子视图上方,因此必须对两个对象的框架进行调整.新视图仍然按照上面列出的方式进行设置,但您需要在现有视图上调用 setFrame:,并将其 y 原点设置为一个值,该值至少测量您的视图的高度已在其上方添加.

Since you want to move this view above an existing subview, you have to make adjustments to the frames of both objects. The new view and still be set up as listed above, but you would need to call setFrame: on the existing view, and set its y origin to a value measuring at least the height of the view that you've added above it.

这篇关于在 UIScrollView iOS 原点上方添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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