未显示UIVcrollView与UIViews

时间:2013-05-28 07:21:23

标签: objective-c uiview uiscrollview scroll

我正在尝试将UIScrollView与UIViews一起使用(将它们作为xib文件进行管理),现在我有一个存储视图的数组。

testingView是一个UIViewcontroller,有自己的xib文件,testingView2也是UIViewcontroller,有自己不同的xib文件

- (void) viewDidLoad {

[super viewDidLoad];

_testingView = [[TestingViewController alloc] init];
_testingView2 = [[TestingViewController2 alloc] init];

self.array = [[NSArray alloc] initWithObjects: _testingView.view, _testingView2.view, nil];

for (int i = 0; i < [array count]; i++) {
CGRect frame;
frame.origin.x = self.scrollingView.frame.size.width*i;
frame.origin.y = 0;
frame.size = self.scrollingView.frame.size;

[self.scrollingView addSubView: [self.array objectAtIndex:i]];

}

self.scrollingView.contentSize = CGSizeMake (_scrollingView.frame.size.width*[array count], _scrollingView.frame.size.height);

}

问题是他只采取了第一个视图(即使我切换它们)并且没有显示第二个视图,与UIImages相同而不是视图工作,有人可以解释我为什么?好像他没有看到数组顺序中的第二个视图...

3 个答案:

答案 0 :(得分:0)

for循环中,您为新添加的子视图计算帧,但不将该帧设置为该子视图。

在你的循环中,像这样添加你的子视图:

UIView *subview = (UIView *)[array objectAtIndex:i];
subview.frame = frame;
[self.scrollingView addSubView:subview];

答案 1 :(得分:0)

您需要在滚动视图中为每个子视图设置框架。例如,您可以将_testingView2.view的框架设置为x与_testingView1.view框架宽度的偏移量。

_testingView2.view.frame = CGRectOffset(_testingView2.frame, CGRectGetWidth(_testingView1.frame), 0)

计算框架的方法有很多种。尝试使用CGRectWidth,CGRectGetMaxX等方法。

答案 2 :(得分:0)

为什么在不使用for循环时计算变量“frame”?

在我看来,您应该对代码进行以下更改。

for (int i = 0; i < [array count]; i++) {
    CGRect frame;
    frame.origin.x = self.scrollingView.frame.size.width*i;
    frame.origin.y = 0;
    frame.size = self.scrollingView.frame.size;

    **UIView *v=[self.array objectAtIndex:i];**

   **v.frame=frame;**


  **[self.scrollingView addSubView:v];**

}

希望它有效。