Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  2. {
  3.  
  4. return 5.0;
  5. }
  6.  
  7. - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
  8. NSArray *answer = [super layoutAttributesForElementsInRect:rect];
  9.  
  10. for(int i = 1; i < [answer count]; ++i) {
  11. UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
  12. UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
  13. NSInteger maximumSpacing = 4;
  14. NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
  15.  
  16. if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
  17. CGRect frame = currentLayoutAttributes.frame;
  18. frame.origin.x = origin + maximumSpacing;
  19. currentLayoutAttributes.frame = frame;
  20. }
  21. }
  22. return answer;
  23. }
  24.  
  25. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  26. flow.itemSize = CGSizeMake(cellWidth, cellHeight);
  27. flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  28. flow.minimumInteritemSpacing = 0;
  29. flow.minimumLineSpacing = 0;
  30.  
  31. - (UIEdgeInsets)collectionView:(UICollectionView *) collectionView
  32. layout:(UICollectionViewLayout *) collectionViewLayout
  33. insetForSectionAtIndex:(NSInteger) section {
  34.  
  35. return UIEdgeInsetsMake(0, 0, 0, 5); // top, left, bottom, right
  36. }
  37.  
  38. - (CGFloat)collectionView:(UICollectionView *) collectionView
  39. layout:(UICollectionViewLayout *) collectionViewLayout
  40. minimumInteritemSpacingForSectionAtIndex:(NSInteger) section {
  41. return 5.0;
  42. }
  43.  
  44. class CustomViewFlowLayout : UICollectionViewFlowLayout {
  45.  
  46. let cellSpacing:CGFloat = 4
  47.  
  48. override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  49. if let attributes = super.layoutAttributesForElementsInRect(rect) {
  50. for (index, attribute) in attributes.enumerate() {
  51. if index == 0 { continue }
  52. let prevLayoutAttributes = attributes[index - 1]
  53. let origin = CGRectGetMaxX(prevLayoutAttributes.frame)
  54. if(origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize().width) {
  55. attribute.frame.origin.x = origin + cellSpacing
  56. }
  57. }
  58. return attributes
  59. }
  60. return nil
  61. }
  62. }
  63.  
  64. - (CGFloat)collectionView:(UICollectionView *)collectionView
  65. layout:(UICollectionViewLayout*)collectionViewLayout
  66. minimumLineSpacingForSectionAtIndex:(NSInteger)section
  67. {
  68. return 20;
  69. }
  70.  
  71. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  72.  
  73. return 20
  74. }
  75.  
  76. [self.collectionView setMinimumLineSpacing:5];
  77.  
  78. [self.collectionView setMinimumInteritemSpacing:5];
  79.  
  80. class DashboardViewController: UIViewController {
  81. @IBOutlet weak var dashboardCollectionView: UICollectionView!
  82.  
  83. override func viewDidLoad() {
  84. super.viewDidLoad()
  85. dashboardCollectionView.delegate = self
  86. }
  87. }
  88.  
  89. extension DashboardViewController: UICollectionViewDelegateFlowLayout {
  90. fileprivate var sectionInsets: UIEdgeInsets {
  91. return .zero
  92. }
  93.  
  94. fileprivate var itemsPerRow: CGFloat {
  95. return 2
  96. }
  97.  
  98. fileprivate var interitemSpace: CGFloat {
  99. return 5.0
  100. }
  101.  
  102. func collectionView(_ collectionView: UICollectionView,
  103. layout collectionViewLayout: UICollectionViewLayout,
  104. sizeForItemAt indexPath: IndexPath) -> CGSize {
  105. let sectionPadding = sectionInsets.left * (itemsPerRow + 1)
  106. let interitemPadding = max(0.0, itemsPerRow - 1) * interitemSpace
  107. let availableWidth = collectionView.bounds.width - sectionPadding - interitemPadding
  108. let widthPerItem = availableWidth / itemsPerRow
  109.  
  110. return CGSize(width: widthPerItem, height: widthPerItem)
  111. }
  112.  
  113. func collectionView(_ collectionView: UICollectionView,
  114. layout collectionViewLayout: UICollectionViewLayout,
  115. insetForSectionAt section: Int) -> UIEdgeInsets {
  116. return sectionInsets
  117. }
  118.  
  119. func collectionView(_ collectionView: UICollectionView,
  120. layout collectionViewLayout: UICollectionViewLayout,
  121. minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  122. return 0.0
  123. }
  124.  
  125. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  126. return interitemSpace
  127. }
  128. }
  129.  
  130. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  131. {
  132. return UIEdgeInsetsMake(5, 5, 5, 5);
  133. }
  134.  
  135. private let minItemSpacing: CGFloat = 8
  136. private let itemWidth: CGFloat = 100
  137. private let headerHeight: CGFloat = 32
  138.  
  139. override func viewDidLayoutSubviews() {
  140. super.viewDidLayoutSubviews()
  141.  
  142. // Create our custom flow layout that evenly space out the items, and have them in the center
  143. let layout = UICollectionViewFlowLayout()
  144. layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
  145. layout.minimumInteritemSpacing = minItemSpacing
  146. layout.minimumLineSpacing = minItemSpacing
  147. layout.headerReferenceSize = CGSize(width: 0, height: headerHeight)
  148.  
  149. // Find n, where n is the number of item that can fit into the collection view
  150. var n: CGFloat = 1
  151. let containerWidth = collectionView.bounds.width
  152. while true {
  153. let nextN = n + 1
  154. let totalWidth = (nextN*itemWidth) + (nextN-1)*minItemSpacing
  155. if totalWidth > containerWidth {
  156. break
  157. } else {
  158. n = nextN
  159. }
  160. }
  161.  
  162. // Calculate the section inset for left and right.
  163. // Setting this section inset will manipulate the items such that they will all be aligned horizontally center.
  164. let inset = max(minItemSpacing, floor( (containerWidth - (n*itemWidth) - (n-1)*minItemSpacing) / 2 ) )
  165. layout.sectionInset = UIEdgeInsets(top: minItemSpacing, left: inset, bottom: minItemSpacing, right: inset)
  166.  
  167. collectionView.collectionViewLayout = layout
  168. }
  169.  
  170. float currentTotalWidth = CollectionView.Frame.Width - Layout.SectionInset.Left - Layout.SectionInset.Right (Layout = flowlayout)
  171. int amountOfCellsPerRow = (currentTotalWidth + MinimumSpacing) / (cell width + MinimumSpacing)
  172.  
  173. float totalWidth =(amountOfCellsPerRow * cell width) + (amountOfCellsPerRow-1) * MinimumSpacing
  174.  
  175. float difference = currentTotalWidth - totalWidth;
  176.  
  177. Layout.SectionInset.Right = Layout.SectionInset.Right + difference;
  178.  
  179. #define cellSize 90
  180.  
  181. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  182. float width = collectionView.frame.size.width;
  183. float spacing = [self collectionView:collectionView layout:collectionViewLayout minimumInteritemSpacingForSectionAtIndex:section];
  184. int numberOfCells = (width + spacing) / (cellSize + spacing);
  185. int inset = (width + spacing - numberOfCells * (cellSize + spacing) ) / 2;
  186.  
  187. return UIEdgeInsetsMake(0, inset, 0, inset);
  188. }
  189.  
  190. class CustomViewFlowLayout : UICollectionViewFlowLayout {
  191.  
  192. let cellSpacing:CGFloat = 4
  193.  
  194. override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  195. let original = super.layoutAttributesForElementsInRect(rect)
  196.  
  197. if let original = original {
  198. let attributes = NSArray.init(array: original, copyItems: true) as! [UICollectionViewLayoutAttributes]
  199.  
  200. for (index, attribute) in attributes.enumerate() {
  201. if index == 0 { continue }
  202. let prevLayoutAttributes = attributes[index - 1]
  203. let origin = CGRectGetMaxX(prevLayoutAttributes.frame)
  204. if(origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize().width) {
  205. attribute.frame.origin.x = origin + cellSpacing
  206. }
  207. }
  208. return attributes
  209. }
  210. return nil
  211. }
  212. }
  213.  
  214. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  215. guard let answer = super.layoutAttributesForElements(in: rect) else { return nil }
  216.  
  217. for i in 1..<answer.count {
  218. let currentAttributes = answer[i]
  219. let previousAttributes = answer[i - 1]
  220.  
  221. let maximumSpacing: CGFloat = 8
  222. let origin = previousAttributes.frame.maxX
  223.  
  224. if (origin + maximumSpacing + currentAttributes.frame.size.width < self.collectionViewContentSize.width && currentAttributes.frame.origin.x > previousAttributes.frame.origin.x) {
  225. var frame = currentAttributes.frame
  226. frame.origin.x = origin + maximumSpacing
  227. currentAttributes.frame = frame
  228. }
  229. }
  230.  
  231. return answer
  232. }
  233.  
  234. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
  235. layout:(UICollectionViewLayout*)collectionViewLayout
  236. insetForSectionAtIndex:(NSInteger)section {
  237.  
  238. UIEdgeInsets insets = UIEdgeInsetsMake(?, ?, ?, ?);
  239.  
  240. return insets;
  241. }
  242.  
  243. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  244. let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
  245. var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
  246. // use a value to keep track of left margin
  247. var leftMargin: CGFloat = 0.0;
  248. for attributes in attributesForElementsInRect! {
  249. let refAttributes = attributes
  250. // assign value if next row
  251. if (refAttributes.frame.origin.x == self.sectionInset.left) {
  252. leftMargin = self.sectionInset.left
  253. } else {
  254. // set x position of attributes to current margin
  255. var newLeftAlignedFrame = refAttributes.frame
  256. newLeftAlignedFrame.origin.x = leftMargin
  257. refAttributes.frame = newLeftAlignedFrame
  258. }
  259. // calculate new value for current margin
  260. leftMargin += refAttributes.frame.size.width + 10
  261. newAttributesForElementsInRect.append(refAttributes)
  262. }
  263.  
  264. return newAttributesForElementsInRect
  265. }
  266.  
  267. @interface MaxSpacingCollectionViewFlowLayout : UICollectionViewFlowLayout
  268. @property (nonatomic,assign) CGFloat maxCellSpacing;
  269. @end
  270.  
  271. @implementation MaxSpacingCollectionViewFlowLayout
  272.  
  273. - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
  274. NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  275.  
  276. if (attributes.count <= 0) return attributes;
  277.  
  278. CGFloat firstCellOriginX = ((UICollectionViewLayoutAttributes *)attributes[0]).frame.origin.x;
  279.  
  280. for(int i = 1; i < attributes.count; i++) {
  281. UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
  282. if (currentLayoutAttributes.frame.origin.x == firstCellOriginX) { // The first cell of a new row
  283. continue;
  284. }
  285.  
  286. UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
  287. CGFloat prevOriginMaxX = CGRectGetMaxX(prevLayoutAttributes.frame);
  288. if ((currentLayoutAttributes.frame.origin.x - prevOriginMaxX) > self.maxCellSpacing) {
  289. CGRect frame = currentLayoutAttributes.frame;
  290. frame.origin.x = prevOriginMaxX + self.maxCellSpacing;
  291. currentLayoutAttributes.frame = frame;
  292. }
  293. }
  294. return attributes;
  295. }
  296.  
  297. @end
  298.  
  299. lazy var collectionView: UICollectionView = {
  300. let layout = UICollectionViewFlowLayout()
  301. let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
  302. cv.backgroundColor = UIColor.rgb(red: 227, green: 227, blue: 227)
  303. cv.showsVerticalScrollIndicator = false
  304. layout.scrollDirection = .vertical
  305. layout.minimumLineSpacing = 1
  306. layout.minimumInteritemSpacing = 1
  307. return cv
  308. }()
  309.  
  310. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  311.  
  312. switch UIDevice.current.modelName {
  313. case "iPhone 4":
  314. return CGSize(width: 106, height: 106)
  315. case "iPhone 5":
  316. return CGSize(width: 106, height: 106)
  317. case "iPhone 6,7":
  318. return CGSize(width: 124, height: 124)
  319. case "iPhone Plus":
  320. return CGSize(width: 137, height: 137)
  321. default:
  322. return CGSize(width: frame.width / 3, height: frame.width / 3)
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement