Comments (5)
I just realized it's trivial to support this for horizontal scrolling using content inset .. Would be great if it just worked with your code out of the box though :)
from ktcenterflowlayout.
@Mazyod How did you adapt this to work for horizontal scrolling?
from ktcenterflowlayout.
@matthewmoss I didn't. I simply replaced KTCenterFlowLayout
with UICollectionViewFlowLayout
for horizontal scrolling, then you simply set the layout insets to:
let cellsTotalWidth = cellsCount * (cellWidth + margin) - margin
let leftInset = min(0, (collectionView.bounds.width - cellsTotalWidth) / 2)
from ktcenterflowlayout.
Hey @Mazyod @matthewmoss, one of you guys should make a layout that does it! If your use case is a simple/singular row. The layout would only need to adjust each UICollectionViewLayoutAttributes
to have the same center as the collection view. Something like this:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attrs = [super layoutAttributesForItemAtIndexPath:indexPath];
CGFloat centerY = self.collectionView.center.y; // adjust for content insets/...
CGPoint center = attrs.center;
center.y = centerY;
attrs.center = center;
return attrs;
}
To vertically items within multiple rows, it'd be more similar to this layout. Feel free to take a stab. I don't think I'll have time to flesh that out.
from ktcenterflowlayout.
As an added note, I managed to do an horizontal layout using a UICollectionFlowLayout
and messing with the insets:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
static const CGFloat cellSpacing = 2.0f;
static const CGFloat cellWidth = 98.0f;
if ( collectionView == self.collectionView ) { //
NSInteger viewWidth = collectionView.bounds.size.width;
NSInteger numberOfCells = [self collectionView:collectionView numberOfItemsInSection:0] ?: 1; // The number of cells. Ideally you would get this from a NSArray instead of this method (at least one element): NSInteger numberOfCells = self.yourData.count ?: 1;
NSInteger totalCellWidth = cellWidth * ( numberOfCells );
NSInteger totalSpacingWidth = cellSpacing * ( numberOfCells - 1 );
CGFloat leftInset;
if ( totalSpacingWidth + totalCellWidth > viewWidth ) {
leftInset = cellSpacing;
}
else {
leftInset = ( viewWidth % ( totalCellWidth + totalSpacingWidth ) ) / 2;
}
leftInset = MAX( 0, leftInset );
NSInteger rightInset = leftInset;
return UIEdgeInsetsMake(0, leftInset, 0, rightInset);
}
return UIEdgeInsetsZero; // Default insets for unhandled UICollectionViews
}
Implementing the UICollectionViewDelegateFlowLayout
is, of course, mandatory:
@interface YourViewController : UIViewController <UICollectionViewDelegateFlowLayout>
from ktcenterflowlayout.
Related Issues (20)
- Cell layout overlap HOT 1
- Still 0.0.2 in cocoapods HOT 2
- iOS9 - UICollectionViewFlowLayout has cached frame mismatch for index path HOT 1
- Autolayout constraints being ignored? HOT 2
- Left alignment. HOT 1
- Support for vertical scroll direction HOT 1
- issue with UICollectionElementKindSectionHeader HOT 2
- self-sizing cells in 1.2 HOT 1
- Fabric crashlytics `layoutAttributesForElementsInRect` crash HOT 1
- when headerAttrs.frame.size is zero,it will be add to updatedAttributes and lead to a crash in IGListKit HOT 3
- Align items to left if content size exceeds bounds HOT 1
- use of unresolved identifier HOT 2
- Support for negative minimumLineSpacing
- Help in using this with swift!!
- Centre cells vertically
- Help to use left align
- Example doesn't work
- Does not work properly with auto layout cell sizing HOT 6
- You can add swift version of this beautiful Layout. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ktcenterflowlayout.