GithubHelp home page GithubHelp logo

Horizontal scrolling about ktcenterflowlayout HOT 5 CLOSED

Mazyod avatar Mazyod commented on May 14, 2024
Horizontal scrolling

from ktcenterflowlayout.

Comments (5)

Mazyod avatar Mazyod commented on May 14, 2024

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.

matthewmoss avatar matthewmoss commented on May 14, 2024

@Mazyod How did you adapt this to work for horizontal scrolling?

from ktcenterflowlayout.

Mazyod avatar Mazyod commented on May 14, 2024

@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.

keighl avatar keighl commented on May 14, 2024

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.

alejandroivan avatar alejandroivan commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.