如何阻止UIScrollView在视图顶部暂时向下滚动

时间:2011-11-19 19:10:45

标签: ios

以下代码(在UIView类中)在屏幕顶部显示一个红色方块。 代码允许用户向下滚动屏幕,使红色方块在顶部消失,屏幕停留在用户滚动到的位置。

问题是:用户还可以滚动 UP ,以便红色方块暂时朝向页面中间(在回到原位之前)。这不应该发生,因为红色方块已放置在视图的顶部。如何正确编码以避免这种情况?非常感谢!

- (id)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
       UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
       scrollView.canCancelContentTouches=NO;

       UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1280)];
       CGRect rectForBigRedSquare = CGRectMake(10, 10, 200, 200);
       UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
       [redSquare setBackgroundColor:[UIColor redColor]];
       [contentView addSubview:redSquare];
       [redSquare release];

       CGSize contentViewSize = contentView.frame.size;
       [scrollView addSubview:contentView];
       [scrollView setContentSize:contentViewSize];
       [contentView release];

       [self addSubview:scrollView];
    }
    return self;
}

1 个答案:

答案 0 :(得分:2)

scrollView.bounces = NO;

顺便说一下,使用'up'和'down'来描述滚动与通常在这种情况下如何描述滚动相反。在iOS中,当用户“向上滚动”意味着他们正在向上推动内容时,顶部的正方形将从顶部消失,而不是向下滚动到中间。 :)

相关问题