From 5541bd043de812103f62a4c621101c3c495d82b8 Mon Sep 17 00:00:00 2001 From: yiplee Date: Tue, 21 Nov 2017 18:21:29 +0800 Subject: [PATCH] Add new iOS 11 api adjustedContentInset support --- IGListKit.xcodeproj/project.pbxproj | 8 +++++++ Source/IGListAdapter.m | 16 +++++++------- Source/IGListCollectionViewLayout.mm | 5 +++-- Source/Internal/UIScrollView+IGListKit.h | 16 ++++++++++++++ Source/Internal/UIScrollView+IGListKit.m | 27 ++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 Source/Internal/UIScrollView+IGListKit.h create mode 100644 Source/Internal/UIScrollView+IGListKit.m diff --git a/IGListKit.xcodeproj/project.pbxproj b/IGListKit.xcodeproj/project.pbxproj index a17cc1be9..03086f0f0 100644 --- a/IGListKit.xcodeproj/project.pbxproj +++ b/IGListKit.xcodeproj/project.pbxproj @@ -351,6 +351,8 @@ DAA83ACF1E8ECE06000F6810 /* IGListAdapter+UICollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5F484A1E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.m */; }; DAD4A40D1E8E9E1E00DACC36 /* IGListAdapter+UICollectionView.h in Headers */ = {isa = PBXBuildFile; fileRef = DA5F48491E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.h */; settings = {ATTRIBUTES = (Private, ); }; }; DD468D380BBF350ACE7EA28B /* Pods_IGListKitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AB7195278D0BBB5DA88D36F /* Pods_IGListKitTests.framework */; }; + E8B5C73B1FC431D400C26914 /* UIScrollView+IGListKit.h in Headers */ = {isa = PBXBuildFile; fileRef = E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */; }; + E8B5C73C1FC431D400C26914 /* UIScrollView+IGListKit.m in Sources */ = {isa = PBXBuildFile; fileRef = E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -585,6 +587,8 @@ DA5F48491E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "IGListAdapter+UICollectionView.h"; sourceTree = ""; }; DA5F484A1E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "IGListAdapter+UICollectionView.m"; sourceTree = ""; }; DD3152AC1DE227FA00AC9D2C /* IGListKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = IGListKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIScrollView+IGListKit.h"; sourceTree = ""; }; + E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIScrollView+IGListKit.m"; sourceTree = ""; }; E980179F5E885E417EB20D55 /* Pods_IGListKit_tvOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_IGListKit_tvOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -764,6 +768,8 @@ 290DF3571E930CE2009FE456 /* UICollectionView+DebugDescription.m */, 0B3B92C21E08D7F5008390ED /* UICollectionView+IGListBatchUpdateData.h */, 0B3B92C31E08D7F5008390ED /* UICollectionView+IGListBatchUpdateData.m */, + E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */, + E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */, ); path = Internal; sourceTree = ""; @@ -1025,6 +1031,7 @@ 290DF3741E931B57009FE456 /* IGListDebuggingUtilities.h in Headers */, 0B3B92EC1E08D7F5008390ED /* IGListMoveIndexPathInternal.h in Headers */, 0B3B93341E08D7F5008390ED /* IGListDisplayHandler.h in Headers */, + E8B5C73B1FC431D400C26914 /* UIScrollView+IGListKit.h in Headers */, 0B3B92D21E08D7F5008390ED /* IGListIndexPathResult.h in Headers */, 2926586C1E75E01A0041B56D /* IGListBindingSectionControllerDataSource.h in Headers */, 0B3B93241E08D7F5008390ED /* IGListSupplementaryViewSource.h in Headers */, @@ -1546,6 +1553,7 @@ 0B3B93421E08D7F5008390ED /* IGListWorkingRangeHandler.mm in Sources */, 0B3B92F01E08D7F5008390ED /* NSNumber+IGListDiffable.m in Sources */, 298DDA211E3B0DC800F76F50 /* IGListCollectionViewLayout.mm in Sources */, + E8B5C73C1FC431D400C26914 /* UIScrollView+IGListKit.m in Sources */, 0B3B93301E08D7F5008390ED /* IGListAdapterProxy.m in Sources */, 0B3B92CC1E08D7F5008390ED /* IGListDiff.mm in Sources */, 0D52F0B61F1AE8BD00460F79 /* IGListBindingSectionController+DebugDescription.m in Sources */, diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index e76b727a6..6c5a69d7a 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -15,6 +15,7 @@ #import "IGListSectionControllerInternal.h" #import "IGListDebugger.h" +#import "UIScrollView+IGListKit.h" @implementation IGListAdapter { NSMapTable *_viewSectionControllerMap; @@ -247,7 +248,7 @@ - (void)scrollToObject:(id)object const CGFloat offsetMid = (offsetMin + offsetMax) / 2.0; const CGFloat collectionViewWidth = collectionView.bounds.size.width; const CGFloat collectionViewHeight = collectionView.bounds.size.height; - const UIEdgeInsets contentInset = collectionView.contentInset; + const UIEdgeInsets contentInset = collectionView.IG_contentInsets; CGPoint contentOffset = collectionView.contentOffset; switch (scrollDirection) { case UICollectionViewScrollDirectionHorizontal: { @@ -268,8 +269,8 @@ - (void)scrollToObject:(id)object contentOffset.x = offsetMin - contentInset.left; break; } - const CGFloat maxOffsetX = collectionView.contentSize.width - collectionView.frame.size.width + collectionView.contentInset.right; - const CGFloat minOffsetX = -collectionView.contentInset.left; + const CGFloat maxOffsetX = collectionView.contentSize.width - collectionView.frame.size.width + contentInset.right; + const CGFloat minOffsetX = -contentInset.left; contentOffset.x = MIN(contentOffset.x, maxOffsetX); contentOffset.x = MAX(contentOffset.x, minOffsetX); break; @@ -292,8 +293,8 @@ - (void)scrollToObject:(id)object contentOffset.y = offsetMin - contentInset.top; break; } - const CGFloat maxOffsetY = collectionView.contentSize.height - collectionView.frame.size.height + collectionView.contentInset.bottom; - const CGFloat minOffsetY = -collectionView.contentInset.top; + const CGFloat maxOffsetY = collectionView.contentSize.height - collectionView.frame.size.height + contentInset.bottom; + const CGFloat minOffsetY = -contentInset.top; contentOffset.y = MIN(contentOffset.y, maxOffsetY); contentOffset.y = MAX(contentOffset.y, minOffsetY); break; @@ -828,12 +829,11 @@ - (CGSize)containerSize { } - (UIEdgeInsets)containerInset { - return self.collectionView.contentInset; + return self.collectionView.IG_contentInsets; } - (CGSize)insetContainerSize { - UICollectionView *collectionView = self.collectionView; - return UIEdgeInsetsInsetRect(collectionView.bounds, collectionView.contentInset).size; + return UIEdgeInsetsInsetRect(self.collectionView.bounds, self.containerInset).size; } - (CGSize)containerSizeForSectionController:(IGListSectionController *)sectionController { diff --git a/Source/IGListCollectionViewLayout.mm b/Source/IGListCollectionViewLayout.mm index 37df6e500..323f7e906 100644 --- a/Source/IGListCollectionViewLayout.mm +++ b/Source/IGListCollectionViewLayout.mm @@ -9,6 +9,7 @@ #import "IGListCollectionViewLayout.h" #import "IGListCollectionViewLayoutInternal.h" +#import "UIScrollView+IGListKit.h" #import @@ -301,7 +302,7 @@ - (CGSize)collectionViewContentSize { const IGListSectionEntry section = _sectionData[sectionCount - 1]; UICollectionView *collectionView = self.collectionView; - const UIEdgeInsets contentInset = collectionView.contentInset; + const UIEdgeInsets contentInset = collectionView.IG_contentInsets; switch (self.scrollDirection) { case UICollectionViewScrollDirectionVertical: { @@ -398,7 +399,7 @@ - (void)cacheLayout { id delegate = (id)collectionView.delegate; const NSInteger sectionCount = [dataSource numberOfSectionsInCollectionView:collectionView]; - const UIEdgeInsets contentInset = collectionView.contentInset; + const UIEdgeInsets contentInset = collectionView.IG_contentInsets; const CGRect contentInsetAdjustedCollectionViewBounds = UIEdgeInsetsInsetRect(collectionView.bounds, contentInset); auto sectionData = std::vector(sectionCount); diff --git a/Source/Internal/UIScrollView+IGListKit.h b/Source/Internal/UIScrollView+IGListKit.h new file mode 100644 index 000000000..ec817e614 --- /dev/null +++ b/Source/Internal/UIScrollView+IGListKit.h @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +@interface UIScrollView (IGListKit) + +- (UIEdgeInsets) IG_contentInsets; + +@end diff --git a/Source/Internal/UIScrollView+IGListKit.m b/Source/Internal/UIScrollView+IGListKit.m new file mode 100644 index 000000000..4aebdf169 --- /dev/null +++ b/Source/Internal/UIScrollView+IGListKit.m @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import "UIScrollView+IGListKit.h" + +@implementation UIScrollView (IGListKit) + +- (UIEdgeInsets) IG_contentInsets +{ +#ifdef __IPHONE_11_0 + if (@available(iOS 11,*)) { + return self.adjustedContentInset; + } else { + return self.contentInset; + } +#else + return self.contentInset; +#endif +} + +@end