Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adaptive Collection View

Yosuke Ishikawa
February 14, 2015
25k

Adaptive Collection View

Yosuke Ishikawa

February 14, 2015
Tweet

Transcript

  1. “Supporting any size display or orientation of a device allows

    you to create a great user experience with your app. With the latest advancements with View Controllers in iOS 8 and Auto Layout in Xcode, it’s now even easier for you to adapt your user interface to context and different sized devices.”
  2. “Supporting any size display or orientation of a device allows

    you to create a great user experience with your app. With the latest advancements with View Controllers in iOS 8 and Auto Layout in Xcode, it’s now even easier for you to adapt your user interface to context and different sized devices.”
  3. “Supporting any size display or orientation of a device allows

    you to create a great user experience with your app. With the latest advancements with View Controllers in iOS 8 and Auto Layout in Xcode, it’s now even easier for you to adapt your user interface to context and different sized devices.”
  4. class CollectionViewLayout: UICollectionViewFlowLayout { enum Mode { case Table case

    Flow } var mode: Mode = (UIDevice.currentDevice().userInterfaceIdiom == .Pad) ? .Flow : .Table }
  5. override var estimatedItemSize: CGSize { get { let spacing =

    minimumLineSpacing var size = super.estimatedItemSize if let width = collectionView?.frame.size.width { switch mode { case .Table: size.width = width case .Flow: size.width = width / 2.0 - spacing } } return size } set { super.estimatedItemSize = newValue } }
  6. override func layoutAttributesForElementsInRect(rect: CGRect) -> [ var attributes = [UICollectionViewLayoutAttributes]()

    if let superAttributes = super.layoutAttributesForElementsInRe for attributes in superAttributes { attributes.frame.origin.x = 0.0 } attributes += superAttributes } return attributes } override func layoutAttributesForItemAtIndexPath(indexPath: NSInde let attributes = super.layoutAttributesForItemAtIndexPath(inde if mode == .Table { attributes.frame.origin.x = 0.0 } return attributes }