Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
- {
- return 5.0;
- }
- - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
- NSArray *answer = [super layoutAttributesForElementsInRect:rect];
- for(int i = 1; i < [answer count]; ++i) {
- UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
- UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
- NSInteger maximumSpacing = 4;
- NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
- if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
- CGRect frame = currentLayoutAttributes.frame;
- frame.origin.x = origin + maximumSpacing;
- currentLayoutAttributes.frame = frame;
- }
- }
- return answer;
- }
- UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
- flow.itemSize = CGSizeMake(cellWidth, cellHeight);
- flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- flow.minimumInteritemSpacing = 0;
- flow.minimumLineSpacing = 0;
- - (UIEdgeInsets)collectionView:(UICollectionView *) collectionView
- layout:(UICollectionViewLayout *) collectionViewLayout
- insetForSectionAtIndex:(NSInteger) section {
- return UIEdgeInsetsMake(0, 0, 0, 5); // top, left, bottom, right
- }
- - (CGFloat)collectionView:(UICollectionView *) collectionView
- layout:(UICollectionViewLayout *) collectionViewLayout
- minimumInteritemSpacingForSectionAtIndex:(NSInteger) section {
- return 5.0;
- }
- class CustomViewFlowLayout : UICollectionViewFlowLayout {
- let cellSpacing:CGFloat = 4
- override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- if let attributes = super.layoutAttributesForElementsInRect(rect) {
- for (index, attribute) in attributes.enumerate() {
- if index == 0 { continue }
- let prevLayoutAttributes = attributes[index - 1]
- let origin = CGRectGetMaxX(prevLayoutAttributes.frame)
- if(origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize().width) {
- attribute.frame.origin.x = origin + cellSpacing
- }
- }
- return attributes
- }
- return nil
- }
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout*)collectionViewLayout
- minimumLineSpacingForSectionAtIndex:(NSInteger)section
- {
- return 20;
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 20
- }
- [self.collectionView setMinimumLineSpacing:5];
- [self.collectionView setMinimumInteritemSpacing:5];
- class DashboardViewController: UIViewController {
- @IBOutlet weak var dashboardCollectionView: UICollectionView!
- override func viewDidLoad() {
- super.viewDidLoad()
- dashboardCollectionView.delegate = self
- }
- }
- extension DashboardViewController: UICollectionViewDelegateFlowLayout {
- fileprivate var sectionInsets: UIEdgeInsets {
- return .zero
- }
- fileprivate var itemsPerRow: CGFloat {
- return 2
- }
- fileprivate var interitemSpace: CGFloat {
- return 5.0
- }
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- sizeForItemAt indexPath: IndexPath) -> CGSize {
- let sectionPadding = sectionInsets.left * (itemsPerRow + 1)
- let interitemPadding = max(0.0, itemsPerRow - 1) * interitemSpace
- let availableWidth = collectionView.bounds.width - sectionPadding - interitemPadding
- let widthPerItem = availableWidth / itemsPerRow
- return CGSize(width: widthPerItem, height: widthPerItem)
- }
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- insetForSectionAt section: Int) -> UIEdgeInsets {
- return sectionInsets
- }
- func collectionView(_ collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 0.0
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
- return interitemSpace
- }
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
- {
- return UIEdgeInsetsMake(5, 5, 5, 5);
- }
- private let minItemSpacing: CGFloat = 8
- private let itemWidth: CGFloat = 100
- private let headerHeight: CGFloat = 32
- override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
- // Create our custom flow layout that evenly space out the items, and have them in the center
- let layout = UICollectionViewFlowLayout()
- layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
- layout.minimumInteritemSpacing = minItemSpacing
- layout.minimumLineSpacing = minItemSpacing
- layout.headerReferenceSize = CGSize(width: 0, height: headerHeight)
- // Find n, where n is the number of item that can fit into the collection view
- var n: CGFloat = 1
- let containerWidth = collectionView.bounds.width
- while true {
- let nextN = n + 1
- let totalWidth = (nextN*itemWidth) + (nextN-1)*minItemSpacing
- if totalWidth > containerWidth {
- break
- } else {
- n = nextN
- }
- }
- // Calculate the section inset for left and right.
- // Setting this section inset will manipulate the items such that they will all be aligned horizontally center.
- let inset = max(minItemSpacing, floor( (containerWidth - (n*itemWidth) - (n-1)*minItemSpacing) / 2 ) )
- layout.sectionInset = UIEdgeInsets(top: minItemSpacing, left: inset, bottom: minItemSpacing, right: inset)
- collectionView.collectionViewLayout = layout
- }
- float currentTotalWidth = CollectionView.Frame.Width - Layout.SectionInset.Left - Layout.SectionInset.Right (Layout = flowlayout)
- int amountOfCellsPerRow = (currentTotalWidth + MinimumSpacing) / (cell width + MinimumSpacing)
- float totalWidth =(amountOfCellsPerRow * cell width) + (amountOfCellsPerRow-1) * MinimumSpacing
- float difference = currentTotalWidth - totalWidth;
- Layout.SectionInset.Right = Layout.SectionInset.Right + difference;
- #define cellSize 90
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- float width = collectionView.frame.size.width;
- float spacing = [self collectionView:collectionView layout:collectionViewLayout minimumInteritemSpacingForSectionAtIndex:section];
- int numberOfCells = (width + spacing) / (cellSize + spacing);
- int inset = (width + spacing - numberOfCells * (cellSize + spacing) ) / 2;
- return UIEdgeInsetsMake(0, inset, 0, inset);
- }
- class CustomViewFlowLayout : UICollectionViewFlowLayout {
- let cellSpacing:CGFloat = 4
- override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- let original = super.layoutAttributesForElementsInRect(rect)
- if let original = original {
- let attributes = NSArray.init(array: original, copyItems: true) as! [UICollectionViewLayoutAttributes]
- for (index, attribute) in attributes.enumerate() {
- if index == 0 { continue }
- let prevLayoutAttributes = attributes[index - 1]
- let origin = CGRectGetMaxX(prevLayoutAttributes.frame)
- if(origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize().width) {
- attribute.frame.origin.x = origin + cellSpacing
- }
- }
- return attributes
- }
- return nil
- }
- }
- override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- guard let answer = super.layoutAttributesForElements(in: rect) else { return nil }
- for i in 1..<answer.count {
- let currentAttributes = answer[i]
- let previousAttributes = answer[i - 1]
- let maximumSpacing: CGFloat = 8
- let origin = previousAttributes.frame.maxX
- if (origin + maximumSpacing + currentAttributes.frame.size.width < self.collectionViewContentSize.width && currentAttributes.frame.origin.x > previousAttributes.frame.origin.x) {
- var frame = currentAttributes.frame
- frame.origin.x = origin + maximumSpacing
- currentAttributes.frame = frame
- }
- }
- return answer
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout*)collectionViewLayout
- insetForSectionAtIndex:(NSInteger)section {
- UIEdgeInsets insets = UIEdgeInsetsMake(?, ?, ?, ?);
- return insets;
- }
- override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
- var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
- // use a value to keep track of left margin
- var leftMargin: CGFloat = 0.0;
- for attributes in attributesForElementsInRect! {
- let refAttributes = attributes
- // assign value if next row
- if (refAttributes.frame.origin.x == self.sectionInset.left) {
- leftMargin = self.sectionInset.left
- } else {
- // set x position of attributes to current margin
- var newLeftAlignedFrame = refAttributes.frame
- newLeftAlignedFrame.origin.x = leftMargin
- refAttributes.frame = newLeftAlignedFrame
- }
- // calculate new value for current margin
- leftMargin += refAttributes.frame.size.width + 10
- newAttributesForElementsInRect.append(refAttributes)
- }
- return newAttributesForElementsInRect
- }
- @interface MaxSpacingCollectionViewFlowLayout : UICollectionViewFlowLayout
- @property (nonatomic,assign) CGFloat maxCellSpacing;
- @end
- @implementation MaxSpacingCollectionViewFlowLayout
- - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
- NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
- if (attributes.count <= 0) return attributes;
- CGFloat firstCellOriginX = ((UICollectionViewLayoutAttributes *)attributes[0]).frame.origin.x;
- for(int i = 1; i < attributes.count; i++) {
- UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
- if (currentLayoutAttributes.frame.origin.x == firstCellOriginX) { // The first cell of a new row
- continue;
- }
- UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
- CGFloat prevOriginMaxX = CGRectGetMaxX(prevLayoutAttributes.frame);
- if ((currentLayoutAttributes.frame.origin.x - prevOriginMaxX) > self.maxCellSpacing) {
- CGRect frame = currentLayoutAttributes.frame;
- frame.origin.x = prevOriginMaxX + self.maxCellSpacing;
- currentLayoutAttributes.frame = frame;
- }
- }
- return attributes;
- }
- @end
- lazy var collectionView: UICollectionView = {
- let layout = UICollectionViewFlowLayout()
- let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
- cv.backgroundColor = UIColor.rgb(red: 227, green: 227, blue: 227)
- cv.showsVerticalScrollIndicator = false
- layout.scrollDirection = .vertical
- layout.minimumLineSpacing = 1
- layout.minimumInteritemSpacing = 1
- return cv
- }()
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- switch UIDevice.current.modelName {
- case "iPhone 4":
- return CGSize(width: 106, height: 106)
- case "iPhone 5":
- return CGSize(width: 106, height: 106)
- case "iPhone 6,7":
- return CGSize(width: 124, height: 124)
- case "iPhone Plus":
- return CGSize(width: 137, height: 137)
- default:
- return CGSize(width: frame.width / 3, height: frame.width / 3)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement