WenchaoD / FSCalendar

A fully customizable iOS calendar library, compatible with Objective-C and Swift
MIT License
10.6k stars 1.94k forks source link

日历控件几率闪退 #541

Open InoruYang opened 7 years ago

InoruYang commented 7 years ago

T

WenchaoD commented 7 years ago

你好,能否提供一个能重现的demo?

InoruYang commented 7 years ago

操作:在点击“选择框”->点击“日历”(一种情景为:点击“选择框”,然后快速点击“日历”,有几率闪退)

在 2017年1月7日,下午4:46,Wenchao Ding notifications@github.com 写道:

你好,能否提供一个能重现的demo?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/WenchaoD/FSCalendar/issues/541#issuecomment-271071390, or mute the thread https://github.com/notifications/unsubscribe-auth/AI3HXZAc-nAwiAPpUjUTrzzlL0RTlXl2ks5rP1DagaJpZM4LdTeE.

nihilenz commented 7 years ago

The same for me by quickly showing on the screen of the calendar (in an app with tab bar controller). Everything ok by waiting some seconds.

0 -[UICollectionViewData _setLayoutAttributes:atGlobalItemIndex:]: 1 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke: 2 -[UICollectionViewData validateLayoutInRect:]: 3 -[UICollectionView layoutSubviews]:

Version 2.6.0 (using CocoaPods)

Edit: It seems that this helps: DispatchQueue.main.async { self.calendar.collectionViewLayout.invalidateLayout() self.calendar.collectionView.collectionViewLayout.invalidateLayout() self.calendar.reloadData() }

InoruYang commented 7 years ago

@nihilenz tha ,Where should I use this code

nihilenz commented 7 years ago

@IceSnow Forget it, it does not always help

WenchaoD commented 7 years ago

Is there someone who can provide a demo project to reproduce this?

avohmincevs commented 7 years ago

This is code that runs inside viewDidLoad for me where I add FSCalendar to existing UIStackView. calendarView variable is a private instance variable. It doesn't happen all the time, around 40-50% of the time.

    // Calendar
    calendarView = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 300)];
    calendarView.dataSource = self;
    calendarView.delegate = self;
    calendarView.scope = FSCalendarScopeMonth;
    calendarView.scopeGesture.enabled = false;
    calendarView.allowsMultipleSelection = true;
    calendarView.scrollEnabled = true;
    calendarView.clipsToBounds = true;
    calendarView.backgroundColor = [[UIColor alloc] initWithRed:0.608 green:0.347 blue:0.714 alpha:1];

    calendarView.appearance.headerTitleColor = [UIColor whiteColor];
    calendarView.appearance.weekdayTextColor = [UIColor whiteColor];
    calendarView.appearance.titleDefaultColor = [UIColor whiteColor];
    calendarView.appearance.eventSelectionColor = [UIColor whiteColor];
    calendarView.appearance.selectionColor = [UIColor whiteColor];

    [calendarView registerClass:FSCalendarCell.self forCellReuseIdentifier:@"cell"];

    // Calendar dates
    now = calendarView.today; // Save today
    calendarView.today = nil; // Hide the today circle
    NSDateComponents *components = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday fromDate:now];

    NSUInteger weekdayToday = [components weekday];
    NSInteger daysToFriday = (13 - weekdayToday) % 7;

    startDate = [gregorian dateByAddingUnit:NSCalendarUnitDay value:daysToFriday toDate:now options:0];
    endDate = [gregorian dateByAddingUnit:NSCalendarUnitDay value:daysToFriday + 2 toDate:now options:0];

    NSDate *current = startDate;

    while ([current compare:endDate] != NSOrderedDescending) {
        [calendarView selectDate:current];
        current = [gregorian dateByAddingUnit:NSCalendarUnitDay value:1 toDate:current options:0];
    }

    constraint = [NSLayoutConstraint constraintWithItem:calendarView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:300];
    [constraint setPriority:999]; // Needs to be less than 1000 not to conflict with UISV-hiding constraint inside stackView
    [constraint setIdentifier:@"calendarViewHeight"];

    [calendarView addConstraint:constraint];
    [calendarView layoutSubviews];

    [stackView insertArrangedSubview:calendarView atIndex:0];
    calendarView.hidden = true;
pcicp commented 7 years ago

@WenchaoD 我也遇到这个BUG过,后来测试发现selectDate在必须要在VC的viewDidAppear及其以后,在更早的生命周期(比方viewDidLoad)就会随机奔溃。

gemmakbarlow commented 7 years ago

Looks like this StackOverflow article may be relevant:

http://stackoverflow.com/questions/18189311/bad-access-on-uicollectionview-setcollectionviewlayoutanimated