Andrew
Andrew

Reputation: 3989

How do I make the contents of a UIScrollView scroll vertically rather than horizontally?

I'm using JTCalendar to build a custom calendar app, and it's set to scroll through the months horizontally by default. From my understanding, setting it to scroll vertically instead would entail laying out the contents (months) in a vertical fashion.

The author of JTcalendar suggested this, but it's unclear how exactly contentOffset should be modified for this purpose. Here are the functions that contain contentOffset:

JTCalendar.m:

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if(self.calendarAppearance.isWeekMode){
        return;
    }

    if(sender == self.menuMonthsView && self.menuMonthsView.scrollEnabled){
        self.contentView.contentOffset = CGPointMake(sender.contentOffset.x * calendarAppearance.ratioContentMenu, self.contentView.contentOffset.y);

    }
    else if(sender == self.contentView && self.contentView.scrollEnabled){
        self.menuMonthsView.contentOffset = CGPointMake(sender.contentOffset.x / calendarAppearance.ratioContentMenu, self.menuMonthsView.contentOffset.y);
    }
}

JTCalendarContentView.m:

- (void)configureConstraintsForSubviews
{
    self.contentOffset = CGPointMake(self.contentOffset.x, 0); // Prevent bug when contentOffset.y is negative

    CGFloat x = 0;
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;

    for(UIView *view in monthsViews){
        view.frame = CGRectMake(x, 0, width, height);
        x = CGRectGetMaxX(view.frame);
    }

    self.contentSize = CGSizeMake(width * NUMBER_PAGES_LOADED, height);
}

Upvotes: 1

Views: 196

Answers (1)

Kris Gellci
Kris Gellci

Reputation: 9687

In scrollViewDidScroll:

This line:

CGPointMake(sender.contentOffset.x * calendarAppearance.ratioContentMenu, self.contentView.contentOffset.y);

Should probably be something like this:

CGPointMake(sender.contentOffset.x, self.contentView.contentOffset.y * calendarAppearance.ratioContentMenu);

And this line:

self.menuMonthsView.contentOffset = CGPointMake(sender.contentOffset.x / calendarAppearance.ratioContentMenu, self.menuMonthsView.contentOffset.y);

Should probably be this:

self.menuMonthsView.contentOffset = CGPointMake(sender.contentOffset.x, self.menuMonthsView.contentOffset.y / calendarAppearance.ratioContentMenu);

In configureConstraintsForSubviews there are a few places that might need modifying. Not sure about the following line since it was set to fix a specific bug, so you could just comment it out for now and see what happens:

// Probably comment this out    
self.contentOffset = CGPointMake(self.contentOffset.x, 0); // Prevent bug when contentOffset.y is negative

This block of code:

for(UIView *view in monthsViews){
        view.frame = CGRectMake(x, 0, width, height);
        x = CGRectGetMaxX(view.frame);
    }

Should probably be something like this: (rename the x variable to y)

for(UIView *view in monthsViews){
    view.frame = CGRectMake(0, y, width, height);
    y = CGRectGetMaxY(view.frame);
}

Last, this line:

self.contentSize = CGSizeMake(width * NUMBER_PAGES_LOADED, height);

Should probably be:

self.contentSize = CGSizeMake(width, height * NUMBER_PAGES_LOADED);

I have not tested any of this but based on the code you posted and the fact that I have used JTCal in the past, this should put you on the right path.

Upvotes: 1

Related Questions