iOS:UIScrollView在底部留空间,如何在顶部留出空间

时间:2017-06-20 19:23:05

标签: ios objective-c uiscrollview contentsize intrinsic-content-size

我有scrollView,其中从底部添加子视图,比如在墙上添加砖块(在我的情况下只有一块砖),然后在其上面的下一块砖,就像那样。与默认UIScrollView ContentSize一样,滚动将在顶部发生。如何滚动到底部。如果我向下滚动,我应该能够看到一个在另一个上面添加的其他砖块。

正如您在下面的屏幕截图中看到的那样,开始按钮应该向下而不是向上。

非常感谢任何形式的帮助。

谢谢

示例代码:

CGFloat initialYPoint = 30;
for (OUSTLevel* currentLevel in self.course.sortedLevelsList) {

    NSArray* nibList = [[UINib nibWithNibName:@"OUSTLearningMapPlayLevelCell" bundle:nil] instantiateWithOwner:self options:nil];
    OUSTLearningMapPlayLevelCell* playLevelCell = nil;

    for (UIView* childView in nibList) {
        if ([childView isKindOfClass:[OUSTLearningMapPlayLevelCell class]]) {
            playLevelCell = (OUSTLearningMapPlayLevelCell*)childView;
            break;
        }
    }

    CGFloat cellHeight = CGRectGetHeight(playLevelCell.frame);
    CGFloat yPoint = CGRectGetHeight(self.scrollview.frame) - (cellHeight + initialYPoint);
    CGRect cellFrame = CGRectMake(0, yPoint, viewWidth, cellHeight);

    [playLevelCell setFrame:cellFrame];      
    [self.scrollview addSubview:playLevelCell];

    initialYPoint += cellHeight;

    if (!landingNodeLevelData.locked) {
        [playLevelCell.unlockedView setHidden:NO];
        [playLevelCell.lockedView setHidden:YES];

        // Frame calculation for placing pin
        CGFloat centerPoint = playLevelCell.unlockedView.frame.size.width / 4;
        pinXPoint = centerPoint + 5;
        pinYPoint = yPoint - 30;
    }
}

CGRect pinFrame = CGRectMake(pinXPoint, pinYPoint, 80, 80);

[self.pinImageView setFrame:pinFrame];
[self.scrollview addSubview:self.pinImageView];
[self.view bringSubviewToFront:self.pinImageView];
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, initialYPoint);
self.scrollview.contentInset = UIEdgeInsetsMake(30.0, 0.0, 30.0, 0.0);
CGPoint bottomOffset = CGPointMake(0, self.scrollview.contentSize.height - self.scrollview.bounds.size.height+ 30);

编辑:我没有使用约束布局,我正在计算&设置框架。

First screen

Second screen

Third Screen

enter image description here

1 个答案:

答案 0 :(得分:0)

看起来你可以让自己的事情变得更轻松。

如果您反向“砖块”堆叠的顺序,您可以从自上而下而不是自下而上填充视图...这更符合逻辑且更容易思考约。

这是对您的pastebin代码的修改。它应该工作正常...我没有你的数据/类/ xib视图/等,但我很确定我模拟它所以这将运行没有错误:

// start with 30-pt "padding" at the top
CGFloat initialYPoint = 30;

// reverse the array of OUSTLevel objects so we're going from top-down instead of bottom-up
NSArray *reversed = [[self.course.sortedLevelsList reverseObjectEnumerator] allObjects];

for (OUSTLevel* currentLevel in reversed) {

    NSArray* nibList = [[UINib nibWithNibName:@"OUSTLearningMapPlayLevelCell" bundle:nil] instantiateWithOwner:self options:nil];
    OUSTLearningMapPlayLevelCell* playLevelCell = nil;

    for (UIView* childView in nibList) {
        if ([childView isKindOfClass:[OUSTLearningMapPlayLevelCell class]]) {
            playLevelCell = (OUSTLearningMapPlayLevelCell*)childView;
            break;
        }
    }

    CGFloat cellHeight = CGRectGetHeight(playLevelCell.frame);

    // we're going top-down now, so Y coordinate will be a simple increment
    CGFloat yPoint = initialYPoint;

    CGRect cellFrame = CGRectMake(0, yPoint, viewWidth, cellHeight);

    [playLevelCell setFrame:cellFrame];

    [self.scrollview addSubview:playLevelCell];

    initialYPoint += cellHeight;

    if (!landingNodeLevelData.locked) {
        [playLevelCell.unlockedView setHidden:NO];
        [playLevelCell.lockedView setHidden:YES];

        // Frame calculation for placing pin
        CGFloat centerPoint = playLevelCell.unlockedView.frame.size.width / 4;
        pinXPoint = centerPoint + 5;
        pinYPoint = yPoint - 30;
    }
}

CGRect pinFrame = CGRectMake(pinXPoint, pinYPoint, 80, 80);

[self.pinImageView setFrame:pinFrame];
[self.scrollview addSubview:self.pinImageView];
[self.view bringSubviewToFront:self.pinImageView];

// add 30-pt "padding" at the bottom
initialYPoint += 30;

// set contentSize
self.scrollview.contentSize = CGSizeMake(cellWidth, initialYPoint);

// we want to initially see the "Start Rocket" visible at the bottom of the scroll view
CGFloat yOffset = scrollview.contentSize.height - scrollview.bounds.size.height;
scrollview.contentOffset = CGPointMake(0, yOffset);

希望它有所帮助 - 如果有什么不清楚的话,请求离开:)