滚动视图将视图添加到彼此之上

时间:2012-03-13 16:40:45

标签: objective-c cocoa-touch ios4

我可以添加几个滚动视图,其背景颜色已更改,并且工作正常。所以基本上每个视图都有不同的背景颜色。但是,当我尝试为每个视图添加单独的视图时,它会将其添加到另一个上面,我不知道为什么。

UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"View1"];
UIViewController *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"View2"];
UIViewController *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"View3"];


for (NSInteger i=0; i<ScrollViewControllerNumberOfPages; i++) {
    CGFloat yOrigin = i * self.view.frame.size.height;
    CGRect subViewFrame = CGRectMake(yOrigin, 0, pageHeight, pageWidth);
    NSLog(@"Printing Width and Height %f  %f",pageWidth,pageHeight);
    UIView *subView = [[UIView alloc] initWithFrame:subViewFrame];

    // Pick a random colour for the view
    CGFloat randomRed = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomGreen = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomBlue = ((CGFloat)(arc4random() % 1000))/1000;
    subView.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1];
    NSLog(@"printin i %i", i);
    if (i == 0)
    subView = view1.view;
    else if (i == 1)
        subView = view2.view;
[self.scrollView addSubview:subView];
}

1 个答案:

答案 0 :(得分:1)

这是一个黑暗中的镜头,因为你的代码需要一些认真的反思。

您正在做的是分配subview,修改它的框架和背景,然后完全忽略它并将其设置为先前实例化viewcontoller的视图。

这应该有效(在某种程度上):

UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"View1"];
UIViewController *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"View2"];
UIViewController *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"View3"];

for (NSInteger i=0; i<ScrollViewControllerNumberOfPages; i++) {
    CGFloat yOrigin = i * self.view.frame.size.height;
    CGRect subViewFrame = CGRectMake(yOrigin, 0, pageHeight, pageWidth);
    NSLog(@"Printing Width and Height %f  %f",pageWidth,pageHeight);

    UIView *subView;

    if (i == 0)
      subView = view1.view;
    else if (i == 1)
      subView = view2.view;

    subView.frame = subViewFrame;

    // Pick a random colour for the view
    CGFloat randomRed = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomGreen = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomBlue = ((CGFloat)(arc4random() % 1000))/1000;
    subView.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1];
    NSLog(@"printin i %d", i);
    [self.scrollView addSubview:subView];
}

如果您这样做,它可能看起来更好

for (NSInteger i=0; i<ScrollViewControllerNumberOfPages; i++) {
    CGFloat yOrigin = i * self.view.frame.size.height;
    CGRect subViewFrame = CGRectMake(yOrigin, 0, pageHeight, pageWidth);
    NSLog(@"Printing Width and Height %f  %f",pageWidth,pageHeight);

    UIViewController *subViewCont = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"View%d",i+1]];

    UIView *subView = subViewCont.view;

    subView.frame = subViewFrame;

    // Pick a random colour for the view
    CGFloat randomRed = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomGreen = ((CGFloat)(arc4random() % 1000))/1000;
    CGFloat randomBlue = ((CGFloat)(arc4random() % 1000))/1000;
    subView.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1];
    NSLog(@"printin i %d", i);
    [self.scrollView addSubview:subView];
}