iPhone SDK: UIScrollView does not scroll

2019-03-26 01:29发布

I just can't get my scroll view to actually scroll.. Everything displays fine except it just won't scroll..... Here's my code:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
    scrollView.contentSize = CGSizeMake(320, 600);
    scrollView.scrollEnabled = YES;
    scrollView.clipsToBounds = YES;
    [self.view addSubview:scrollView];

    planView = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:@"WorkoutTable.png"]];
    planView2 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:@"WorkoutTable.png"]];
    planView2.frame = CGRectMake(0, 164, 320, 165);
    planView3 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:@"WorkoutTable.png"]];
    planView3.frame = CGRectMake(0, 328, 320, 165);
    planView4 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:@"WorkoutTable.png"]];
    planView4.frame = CGRectMake(0, 505, 320, 165);
    [scrollView addSubview:planView];
    [scrollView addSubview:planView2];
    [scrollView addSubview:planView3];
    [scrollView addSubview:planView4];
}

How do I fix this problem?

2条回答
该账号已被封号
2楼-- · 2019-03-26 02:05

your scrollview frame should be smaller than contentSize,so only you can make scrollview scrollable.

查看更多
放我归山
3楼-- · 2019-03-26 02:15

The contentSize must be larger than the scrollview's frame for there to be anything to scroll. :)

In this case, they're both (320, 600) so change your scrollView's init to

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
查看更多
登录 后发表回答