2015-06-22 114 views
1

我使用JTCalendar來構建一個自定義日曆應用程序,並且它被設置爲在默認情況下水平滾動瀏覽月份。根據我的理解,將其設置爲垂直滾動將需要以垂直方式佈置內容(月)。如何使UIScrollView的內容垂直滾動而不是水平滾動?

JTcalendar的作者建議this,但目前尚不清楚contentOffset應該如何修改。這裏是包含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); 
} 

回答

1

在scrollViewDidScroll:

這條線:

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

也許應該是這樣的:

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

這行:

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

也許應該是這樣的:

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

在configureConstraintsForSubviews有幾個地方是可能需要修改。不知道下面這行,因爲它被設置爲確定一個具體的錯誤,所以你可以只把它註釋掉現在,看看會發生什麼:

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

的代碼塊:

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

大概是這樣的:(重命名變量x與y)

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

末,該行:

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

大概應該是:

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

我沒有測試過任何這一點,但基於您發佈的代碼和我已經在過去使用JTCal的事實,這應該讓你在正確的道路上。

+0

這允許垂直滾動,但是當滾動停止時,日子消失。任何可能發生這種情況的原因? – TheDudeGuy