Skip to content

Commit

Permalink
Add new iOS 11 api adjustedContentInset support
Browse files Browse the repository at this point in the history
  • Loading branch information
yiplee committed Nov 21, 2017
1 parent 2f17cb5 commit 5541bd0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
8 changes: 8 additions & 0 deletions IGListKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -585,6 +587,8 @@
DA5F48491E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "IGListAdapter+UICollectionView.h"; sourceTree = "<group>"; };
DA5F484A1E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "IGListAdapter+UICollectionView.m"; sourceTree = "<group>"; };
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 = "<group>"; };
E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIScrollView+IGListKit.m"; sourceTree = "<group>"; };
E980179F5E885E417EB20D55 /* Pods_IGListKit_tvOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_IGListKit_tvOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
16 changes: 8 additions & 8 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#import "IGListSectionControllerInternal.h"
#import "IGListDebugger.h"
#import "UIScrollView+IGListKit.h"

@implementation IGListAdapter {
NSMapTable<UICollectionReusableView *, IGListSectionController *> *_viewSectionControllerMap;
Expand Down Expand Up @@ -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: {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions Source/IGListCollectionViewLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import "IGListCollectionViewLayout.h"
#import "IGListCollectionViewLayoutInternal.h"
#import "UIScrollView+IGListKit.h"

#import <vector>

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -398,7 +399,7 @@ - (void)cacheLayout {
id<UICollectionViewDelegateFlowLayout> delegate = (id<UICollectionViewDelegateFlowLayout>)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<IGListSectionEntry>(sectionCount);
Expand Down
16 changes: 16 additions & 0 deletions Source/Internal/UIScrollView+IGListKit.h
Original file line number Diff line number Diff line change
@@ -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 <UIKit/UIKit.h>

@interface UIScrollView (IGListKit)

- (UIEdgeInsets) IG_contentInsets;

@end
27 changes: 27 additions & 0 deletions Source/Internal/UIScrollView+IGListKit.m
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5541bd0

Please sign in to comment.