在scrollview中的多个视图副本

时间:2011-12-22 09:46:01

标签: iphone objective-c ios uiview uiscrollview

我有一些Lables& nib文件中的文本字段。我想制作视图的多个副本并添加一个scrollview。当我开始在滚动视图中添加视图时,它将添加到滚动视图的顶部。帮助将不胜感激。 提前致谢。

3 个答案:

答案 0 :(得分:3)

您需要设置要添加到scrollView的视图的框架。

subview.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];

CGRectMake定义了将子视图添加到视图的位置。

CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height );

执行此操作将阻止在scrollView中添加的视图彼此重叠。

答案 1 :(得分:1)

如果希望逐个添加视图,则必须计算视图的高度并修改要添加到滚动的每个新视图的框架。例如,如果您的视图高度为100px,则必须将其添加为:

// Number of views added to the scroll
NSUInteger numberOfViews = 0;

// View loaded from XIB
theNewView.frame = CGRectMake(theNewView.frame.origin.x, 
                              theNewView.frame.origin.y + numberOfViews*theNewView.frame.size.height,
                              theNewView.frame.size.width,
                              theNewView.frame.size.height);


// Add the view to the scroll and increment the number of views
[scrollView addSubview:theNewView];
numberOfViews++;

答案 2 :(得分:1)

也许您需要这些方法?

-sendSubviewToBack:
–bringSubviewToFront: