Glow-Inc / GLCalendarView

A fully customizable calendar view acting as a date range picker
MIT License
855 stars 106 forks source link

Broken constraints. Help. #22

Closed OrWest closed 8 years ago

OrWest commented 8 years ago

Hello, i have trouble with your view. When i set 10 ranges and set begin date to current date, constraints break.

Please, help me as soon as possible. I have to complete this screen yesterday.

2016-04-27 10 44 25

Console:

2016-04-27 10 50 17

Code to create:

- (void)viewDidLoad {
  [super viewDidLoad];

  self.calendarView.delegate = self;
  self.calendarView.showMagnifier = YES;
  if (self.editItinerary) {
    NSDateComponents *components =
    [[NSCalendar currentCalendar] components:NSCalendarUnitYear |
                                             NSCalendarUnitMonth
                                    fromDate:self.editItinerary.startDate];
    components.day = 1;
    self.calendarView.firstDate = [[NSCalendar currentCalendar] dateFromComponents:components];
  } else {
    NSDateComponents *components =
        [[NSCalendar currentCalendar] components:NSCalendarUnitYear |
                                                 NSCalendarUnitMonth
                                        fromDate:[NSDate date]];
    components.day = 1;
    self.calendarView.firstDate = [[NSCalendar currentCalendar] dateFromComponents:components];
  }
  self.calendarView.lastDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*365]; // + 1 years
  self.calendarView.rowHeight =
      (CGRectGetWidth(self.view.bounds) - self.calendarView.padding * 2 + 30) / 7;
  [GLCalendarDayCell appearance].rangeDisplayMode = RANGE_DISPLAY_MODE_CONTINUOUS;

  NSString *title;
  if (self.location) { // It's creating
    title = self.location.shortName;
    [self.placeImageView sd_setImageWithURL:self.location.bigImageURL];
    self.deleteButton.hidden = YES;
  } else { // It's editing already existed itinerary
    title = self.editItinerary.location.shortName;
    [self.placeImageView sd_setImageWithURL:self.editItinerary.location.bigImageURL
     placeholderImage:[UIImage imageNamed:@"ph_town_big"]];
  }
  self.title = title;

  for (LOMItinerary *userItinerary in self.userItineraries) {
    GLCalendarDateRange *range =
        [GLCalendarDateRange rangeWithBeginDate:userItinerary.startDate
                                        endDate:userItinerary.endDate];
    range.editable = NO;
    range.backgroundColor = [UIColor colorWithRed:233.f/255.f
                                            green:233.f/255.f
                                             blue:233.f/255.f
                                            alpha:1.f];
    range.textColor = [UIColor whiteColor];
    [self.calendarView addRange:range];
  }

}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  [self.calendarView reload];
  __weak LOMAddItineraryViewController *weakSelf = self;
  dispatch_async(dispatch_get_main_queue(), ^{
      // It have to be in main async block. Else it doesn't work.
      NSDate *dateToScroll;
      if (weakSelf.location) {
        dateToScroll = [NSDate date];
      } else {
        dateToScroll = weakSelf.editItinerary.startDate;
        GLCalendarDateRange *range =
        [GLCalendarDateRange rangeWithBeginDate:weakSelf.editItinerary.startDate
                                        endDate:weakSelf.editItinerary.endDate];
        range.editable = NO;
        range.backgroundColor = [UIColor colorWithRed:250.f/255.f
                                                green:132.f/255.f
                                                 blue:132.f/255.f
                                                alpha:1.f];
        range.textColor = [UIColor whiteColor];
        [weakSelf.calendarView addRange:range];
        weakSelf.currentRange = range;
      }
      [weakSelf.calendarView scrollToDate:dateToScroll animated:NO];
  });
}
ltebean commented 8 years ago

I think you can fix the problem by: instead of calling [self.calendarView addRange:range];, directly assigned the ranges to the calendarView, like self.calendarView.ranges = [@[range1, range2] mutableCopy]; , then call [calendarView reload]

ltebean commented 8 years ago

because addRange internally will call [self.collectionView reloadItemsAtIndexPaths:indexPaths], and at that time, the collection view are not reloaded yet, so the constraints broke

OrWest commented 8 years ago

Ok. I will try it. Thanks. Now i'm calling this method in viewWillAppear in dispath_async block. And it's working fine.