From 661f376fa547addf1d6cef4013d856e23bb22ff9 Mon Sep 17 00:00:00 2001 From: dingwenchao Date: Sun, 20 Dec 2015 15:53:13 +0800 Subject: [PATCH] Detect orientation in App Extension Fix #162 --- Example/Base.lproj/Main_iPhone.storyboard | 95 +---------------------- FSCalendar/FSCalendar.m | 56 ++++++++----- FSCalendar/FSCalendarStickyHeader.m | 35 +++++---- 3 files changed, 55 insertions(+), 131 deletions(-) diff --git a/Example/Base.lproj/Main_iPhone.storyboard b/Example/Base.lproj/Main_iPhone.storyboard index 2e92871..175dfc5 100644 --- a/Example/Base.lproj/Main_iPhone.storyboard +++ b/Example/Base.lproj/Main_iPhone.storyboard @@ -1,8 +1,8 @@ - + - + @@ -12,7 +12,6 @@ - @@ -30,7 +29,6 @@ - @@ -45,7 +43,6 @@ @@ -271,7 +240,6 @@ - @@ -289,7 +257,6 @@ - @@ -300,17 +267,14 @@ - - @@ -324,15 +288,12 @@ - - @@ -344,15 +305,12 @@ - - @@ -361,7 +319,6 @@ - @@ -404,7 +361,6 @@ - @@ -425,7 +381,6 @@ - @@ -457,7 +412,6 @@ - @@ -472,15 +426,12 @@ - - @@ -492,15 +443,12 @@ - - @@ -512,15 +460,12 @@ - - @@ -536,15 +481,12 @@ - - @@ -560,15 +502,12 @@ - - @@ -580,15 +519,12 @@ - - @@ -603,7 +539,6 @@ - @@ -615,7 +550,6 @@ - @@ -623,7 +557,6 @@ - @@ -635,15 +568,12 @@ - - @@ -659,15 +589,12 @@ - - @@ -679,15 +606,12 @@ - - @@ -699,15 +623,12 @@ - - @@ -719,15 +640,12 @@ - - @@ -739,15 +657,12 @@ - - @@ -759,15 +674,12 @@ - - @@ -779,15 +691,12 @@ - - diff --git a/FSCalendar/FSCalendar.m b/FSCalendar/FSCalendar.m index c4bff83..c52582b 100644 --- a/FSCalendar/FSCalendar.m +++ b/FSCalendar/FSCalendar.m @@ -17,6 +17,11 @@ #import "FSCalendarDynamicHeader.h" #import "FSCalendarCollectionView.h" +typedef NS_ENUM(NSUInteger, FSCalendarOrientation) { + FSCalendarOrientationLandscape, + FSCalendarOrientationPortrait +}; + @interface FSCalendar (DataSourceAndDelegate) - (BOOL)hasEventForDate:(NSDate *)date; @@ -77,11 +82,12 @@ @property (assign, nonatomic) CGFloat preferedHeaderHeight; @property (assign, nonatomic) CGFloat preferedWeekdayHeight; @property (assign, nonatomic) CGFloat preferedRowHeight; -@property (assign, nonatomic) UIInterfaceOrientation interfaceOrientation; +@property (assign, nonatomic) FSCalendarOrientation orientation; @property (readonly, nonatomic) BOOL floatingMode; @property (readonly, nonatomic) BOOL hasValidateVisibleLayout; @property (readonly, nonatomic) NSArray *visibleStickyHeaders; +@property (readonly, nonatomic) FSCalendarOrientation currentCalendarOrientation; @property (readonly, nonatomic) id delegateAppearance; @@ -182,7 +188,7 @@ _needsAdjustingTextSize = YES; _needsAdjustingMonthPosition = YES; _stickyHeaderMapTable = [NSMapTable weakToWeakObjectsMapTable]; - _interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; + _orientation = self.currentCalendarOrientation; UIView *contentView = [[UIView alloc] initWithFrame:CGRectZero]; contentView.backgroundColor = [UIColor clearColor]; @@ -274,7 +280,7 @@ weekdayWidth, weekdayHeight); }]; - + _deliver.frame = CGRectMake(_header.fs_left, _header.fs_top, _header.fs_width, headerHeight+weekdayHeight); _deliver.hidden = _header.hidden; @@ -668,7 +674,7 @@ - (void)orientationDidChange:(NSNotification *)notification { - self.interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; + self.orientation = self.currentCalendarOrientation; } - (void)significantTimeDidChange:(NSNotification *)notification @@ -890,24 +896,22 @@ } } -- (void)setInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation +- (void)setOrientation:(FSCalendarOrientation)orientation { - if (_interfaceOrientation != interfaceOrientation) { - _interfaceOrientation = interfaceOrientation; + if (_orientation != orientation) { + _orientation = orientation; + + _needsAdjustingViewFrame = YES; + _needsAdjustingMonthPosition = YES; + _needsAdjustingTextSize = YES; + _preferedWeekdayHeight = FSCalendarAutomaticDimension; + _preferedRowHeight = FSCalendarAutomaticDimension; + _preferedHeaderHeight = FSCalendarAutomaticDimension; + [self.visibleStickyHeaders setValue:@YES forKey:@"needsAdjustingViewFrame"]; + [_collectionView.visibleCells setValue:@YES forKey:@"needsAdjustingViewFrame"]; + _header.needsAdjustingViewFrame = YES; + [self setNeedsLayout]; - if (interfaceOrientation != UIInterfaceOrientationUnknown) { - _needsAdjustingViewFrame = YES; - _needsAdjustingMonthPosition = YES; - _needsAdjustingTextSize = YES; - _preferedWeekdayHeight = FSCalendarAutomaticDimension; - _preferedRowHeight = FSCalendarAutomaticDimension; - _preferedHeaderHeight = FSCalendarAutomaticDimension; - [self.visibleStickyHeaders setValue:@YES forKey:@"needsAdjustingViewFrame"]; - [_collectionView.visibleCells setValue:@YES forKey:@"needsAdjustingViewFrame"]; - _header.needsAdjustingViewFrame = YES; - [self setNeedsLayout]; - } - } } @@ -1345,7 +1349,6 @@ } else { // 全屏模式中,切换页面时需要将该月份提升到视图最上方 if (self.hasValidateVisibleLayout) { -// [_collectionViewLayout invalidateLayout]; CGRect headerFrame = [_collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:scrollOffset]].frame; CGPoint targetOffset = CGPointMake(0, MIN(headerFrame.origin.y,_collectionView.contentSize.height-_collectionView.fs_bottom)); [_collectionView setContentOffset:targetOffset animated:animated]; @@ -1738,6 +1741,17 @@ [_weekdays makeObjectsPerformSelector:@selector(setTextColor:) withObject:_appearance.weekdayTextColor]; } +// The best way to detect orientation +// http://stackoverflow.com/questions/25830448/what-is-the-best-way-to-detect-orientation-in-an-app-extension/26023538#26023538 +- (FSCalendarOrientation)currentCalendarOrientation +{ + CGFloat scale = [UIScreen mainScreen].scale; + CGSize nativeSize = [UIScreen mainScreen].currentMode.size; + CGSize sizeInPoints = [UIScreen mainScreen].bounds.size; + FSCalendarOrientation orientation = scale * sizeInPoints.width == nativeSize.width ? FSCalendarOrientationPortrait : FSCalendarOrientationLandscape; + return orientation; +} + #pragma mark - Delegate - (BOOL)shouldSelectDate:(NSDate *)date diff --git a/FSCalendar/FSCalendarStickyHeader.m b/FSCalendar/FSCalendarStickyHeader.m index eb406b7..a54aad0 100644 --- a/FSCalendar/FSCalendarStickyHeader.m +++ b/FSCalendar/FSCalendarStickyHeader.m @@ -66,23 +66,24 @@ [super layoutSubviews]; if (_needsAdjustingViewFrame) { - if (!CGSizeEqualToSize(self.frame.size, CGSizeZero)) { - _needsAdjustingViewFrame = NO; - _contentView.frame = self.bounds; - CGFloat weekdayWidth = self.fs_width / 7.0; - CGFloat weekdayHeight = _calendar.preferedWeekdayHeight; - CGFloat weekdayMargin = weekdayHeight * 0.1; - CGFloat titleWidth = _contentView.fs_width; - - [_weekdayLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger index, BOOL *stop) { \ - label.frame = CGRectMake(index*weekdayWidth, _contentView.fs_height-weekdayHeight-weekdayMargin, weekdayWidth, weekdayHeight); - }]; - - CGFloat titleHeight = [@"1" sizeWithAttributes:@{NSFontAttributeName:_appearance.preferredHeaderTitleFont}].height*1.5 + weekdayMargin*3; - - _separator.frame = CGRectMake(0, _contentView.fs_height-weekdayHeight-weekdayMargin*2, _contentView.fs_width, 1.0); - _titleLabel.frame = CGRectMake(0, _separator.fs_bottom-titleHeight-weekdayMargin, titleWidth,titleHeight); - } + + _needsAdjustingViewFrame = NO; + _contentView.frame = self.bounds; + + CGFloat weekdayWidth = self.fs_width / 7.0; + CGFloat weekdayHeight = _calendar.preferedWeekdayHeight; + CGFloat weekdayMargin = weekdayHeight * 0.1; + CGFloat titleWidth = _contentView.fs_width; + + [_weekdayLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger index, BOOL *stop) { \ + label.frame = CGRectMake(index*weekdayWidth, _contentView.fs_height-weekdayHeight-weekdayMargin, weekdayWidth, weekdayHeight); + }]; + + CGFloat titleHeight = [@"1" sizeWithAttributes:@{NSFontAttributeName:_appearance.preferredHeaderTitleFont}].height*1.5 + weekdayMargin*3; + + _separator.frame = CGRectMake(0, _contentView.fs_height-weekdayHeight-weekdayMargin*2, _contentView.fs_width, 1.0); + _titleLabel.frame = CGRectMake(0, _separator.fs_bottom-titleHeight-weekdayMargin, titleWidth,titleHeight); + } [self reloadData];