代码之家  ›  专栏  ›  技术社区  ›  Mike S Andreas Köberle

UIView setBounds UIScrollView地狱!

  •  1
  • Mike S Andreas Köberle  · 技术社区  · 16 年前

    我有一堆“行视图”,我想放在一个垂直滚动视图。我在IB中创建了一个单独的nib行视图。它们的大小是1024/200(ipad)。现在我想把它们一个接一个地放到我的父UIScrollView中。我尝试了一个简单的方法ubview:rowView]但这会使它们相互重叠(为了检查这一点,我将rowview设置为透明的)。因此,我开始愚弄每个行视图的边界,但没有结果。这是我的密码。注意“self.yextensofar”被初始化为0。想象一下为每一行调用下面的代码:

                MyRowView *rowView = [[MyRowView alloc] init];
    
        float calculatedWidth = 0;
        // minus nav bar
        float calculatedHeight = 0;
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIInterfaceOrientationPortrait){
            // iPad
            calculatedWidth =  768.0;
            calculatedHeight = 960.0;
        }else{
            // iPad
            calculatedWidth =  1024.0;
            calculatedHeight = 704.0;
        }
    
        [self.vScroll addSubview: rowView.view];
        [rowView.view setBounds:CGRectMake(0, self.yExtentSoFar, calculatedWidth,200)];
        [self.vScroll setContentSize:CGSizeMake(calculatedWidth, yExtentSoFar+200)];
        self.yExtentSoFar += 200;
    

    因此,在我尝试设置边界之前,行视图似乎彼此重叠。我想可以理解。当我设置边界时,第二行视图并没有像预期的那样出现在第一行下面,相反,我必须下拉vScroll,第二行视图出现在第一个屏幕外的上面!

    有人能指出我错在哪里吗?谢谢,

    2 回复  |  直到 16 年前
        1
  •  3
  •   ohhorob    16 年前

    您想通过设置子视图的 frame

    特别是你混淆了参考坐标。。边界是指要显示多少视图。而框架是;视图应放置在其superview中的大小。

    参见“框架、边界和中心的关系” View Programming Guide for iPhone

        2
  •  2
  •   Matt Long    16 年前

    Use a UITableView 以及自定义表视图单元格。它会让你的生活 许多的 更容易的。