ios 上 self-sizing cell 的過去、現在、與未來

75
iOS Ӥ self-sizing cell ጱ螂牏 匍牏膏๚㬵 ฏਡ(Jeff Lin)

Upload: -

Post on 13-Apr-2017

340 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: iOS 上 self-sizing cell 的過去、現在、與未來

iOS self-sizing cell

(Jeff Lin)

Page 2: iOS 上 self-sizing cell 的過去、現在、與未來

• Yahoo AppDevKit - - (Anistar)

• https://github.com/yahoo/AppDevKit

• – iOS BDD - (qcl)

• https://github.com/qcl/zizhi

Page 3: iOS 上 self-sizing cell 的過去、現在、與未來

About me• YAHOO App Engineer

• iOS App

• AppDevKit

Page 4: iOS 上 self-sizing cell 的過去、現在、與未來

Agenda

• Self-sizing

• Self-sizing

• Xcode 8

Page 5: iOS 上 self-sizing cell 的過去、現在、與未來

Self-sizing

Page 6: iOS 上 self-sizing cell 的過去、現在、與未來
Page 7: iOS 上 self-sizing cell 的過去、現在、與未來
Page 8: iOS 上 self-sizing cell 的過去、現在、與未來

/

Page 9: iOS 上 self-sizing cell 的過去、現在、與未來

/

Page 10: iOS 上 self-sizing cell 的過去、現在、與未來

Expandable cell

Page 11: iOS 上 self-sizing cell 的過去、現在、與未來

• iOS dynamic type

• UIFont.preferredFontForTextStyle:

Page 12: iOS 上 self-sizing cell 的過去、現在、與未來
Page 13: iOS 上 self-sizing cell 的過去、現在、與未來

• UIImageView, UILabel, UITextView…

• Container

• UITableView, UICollectionView

• storyboard, xib, constraint

Page 14: iOS 上 self-sizing cell 的過去、現在、與未來

Layout

Page 15: iOS 上 self-sizing cell 的過去、現在、與未來

Self-sizing ?

Page 16: iOS 上 self-sizing cell 的過去、現在、與未來

!• auto self-sizing

Page 17: iOS 上 self-sizing cell 的過去、現在、與未來

• NSString

• sizeWithAttributes

• boundingRectWithSize:options:attributes:context:

• UILabel

• sizeToFit:

• UIScrollView/UITextView

• contentSize

Page 18: iOS 上 self-sizing cell 的過去、現在、與未來

• size ceil()

• sizeToFit: frameconstraint

Page 19: iOS 上 self-sizing cell 的過去、現在、與未來

UITextView contentSize

• sizeForItemAtIndexPath:

• textView.ContentSize

Page 20: iOS 上 self-sizing cell 的過去、現在、與未來

iOS ?

Page 21: iOS 上 self-sizing cell 的過去、現在、與未來

UITableview self sizing

• UILabel numberOfLines = 0

• AutoLayout

self.tableView?.rowHeight = UITableViewAutomaticDimension self.tableView?.estimatedRowHeight = 100

Page 22: iOS 上 self-sizing cell 的過去、現在、與未來

That’s all!

Page 23: iOS 上 self-sizing cell 的過去、現在、與未來

UITableview self sizing

• layout

Page 24: iOS 上 self-sizing cell 的過去、現在、與未來

UICollectionView

• Grid view,

• /layout

Page 25: iOS 上 self-sizing cell 的過去、現在、與未來

UICollectionView self-sizing

Page 26: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit• UICollectionViewCell

• subclass ADKTableViewDynamicSizeCell/ADKCollectionViewDynamicSizeCell

• constraint

• collectionView:layout:sizeForItemAtIndexPath:

• ADKNibCacheManager cell

• cell

Page 27: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

Page 28: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

collectionView:layout:sizeForItemAtIndexPath:

1. cell

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String? let cell = ADKSWNibCacheManager.sharedInstance().instance( className: appName?.appending(".\(SSSmallCardCollectionViewCell.self)")) as! SSSmallCardCollectionViewCell

Page 29: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

2.

let preferSize = CGSize.init(width: collectionView.frame.size.width / 2.0 - 10, height: 100.0) cell.frame = CGRect.init(origin: .zero, size: preferSize)

3. layoutIfNeeded constraint

cell.contentView.layoutIfNeeded()

Page 30: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

4.

func setup(cell: SSSmallCardCollectionViewCell, indexPath: IndexPath) { let productInfo = self.products[indexPath.row] cell.contentView.layoutIfNeeded() cell.titleLabel?.text = productInfo.productTitle ... }

Page 31: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

5.

let size = ADKCellDynamicSizeCalculator.sharedInstance().size(forDynamicHeightCellInstance: cell, preferredSize: preferSize)

return size

Page 32: iOS 上 self-sizing cell 的過去、現在、與未來

Sample Code@objc(collectionView:layout:sizeForItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

// 1. cell let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String? let cell = ADKSWNibCacheManager.sharedInstance().instance(className: appName?.appending(".\(SSSmallCardCollectionViewCell.self)")) as! SSSmallCardCollectionViewCell

// 2. let preferSize = CGSize.init(width: collectionView.frame.size.width / 2.0 - 10, height: 100.0) cell.frame = CGRect.init(origin: .zero, size: preferSize)

// 3. layoutIfNeeded constraint cell.contentView.layoutIfNeeded()

// 4. setup(cell: cell, indexPath: indexPath)

// 5. let size = ADKCellDynamicSizeCalculator.sharedInstance().size(forDynamicHeightCellInstance: cell, preferredSize: CGSize.init(width: collectionView.frame.size.width / 2.0 - 10, height: 0.0)) return size }

Page 33: iOS 上 self-sizing cell 的過去、現在、與未來

Recap1. cell

2.

3. layoutIfNeededconstraint

4.

5.

ImageView

Title

Description

Price

Page 34: iOS 上 self-sizing cell 的過去、現在、與未來

Recap1. cell

2.

3. layoutIfNeededconstraint

4.

5.

ImageView

Title

Description

Price

Page 35: iOS 上 self-sizing cell 的過去、現在、與未來

Recap1. cell

2.

3. layoutIfNeededconstraint

4.

5.

ImageView

Title

Description

Price

Page 36: iOS 上 self-sizing cell 的過去、現在、與未來

Recap1. cell

2.

3. layoutIfNeededconstraint

4.

5.

ImageView

Title

Description

Price

ImageView

SO NICE

$ 790

Page 37: iOS 上 self-sizing cell 的過去、現在、與未來

ImageView

SO NICE

790

Recap1. cell

2.

3. layoutIfNeededconstraint

4.

5.

ImageView

SO NICE

$ 790

SO NICE

,

,

$ 790

Page 38: iOS 上 self-sizing cell 的過去、現在、與未來

AppDevKit Solution

ADKCellDynamicSizeCalculator.sharedInstance().size(forDynamicHeightCellInstance: cell, preferredSize: CGSize.init(width: collectionView.frame.size.width / 2.0 - 10, height: 0.0))

10px

Page 39: iOS 上 self-sizing cell 的過去、現在、與未來

How AppDevKit works?•CGSize resultSize = [contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; resultSize.width = preferredSize.width; resultSize.height = MAX(resultSize.height, preferredSize.height);

Page 40: iOS 上 self-sizing cell 的過去、現在、與未來

systemLayoutSizeFittingSize

• UILayoutFittingCompressedSize

• view constraints size

• UILayoutFittingExpandedSize

• view constraints size

Page 41: iOS 上 self-sizing cell 的過去、現在、與未來

layoutIfNeeds

• layoutIfNeeds

• reloadData contentSize

• Animate autoLayout

Page 42: iOS 上 self-sizing cell 的過去、現在、與未來

contentSize

// contentSize: {0, 0}collectionView.dataSource = self.delegator// contentSize: {0, 0}collectionView.reloadData()// contentSize: {0, 0}collectionView.layoutIfNeeded// contentSize: {375, 2557.5}

Page 43: iOS 上 self-sizing cell 的過去、現在、與未來

layoutIfNeeds-Animation

viewForAnimate.someConstraint.constanst = 0.0 UIView.animate(withDuration: 0.3) {

viewForAnimate.layoutIfNeeded() }

UIView.animate(withDuration: 0.3) { viewForAnimate.frame = CGRect.init(origin: .zero, size: CGSize.init(width: 100, height: 100))

}

Frame base layout

Constraint Autolayout

Page 44: iOS 上 self-sizing cell 的過去、現在、與未來

iOS ?

Page 45: iOS 上 self-sizing cell 的過去、現在、與未來

cell

• collectionView:layout:sizeForItemAtIndexPath:

• Customized UICollectionViewLayout

Page 46: iOS 上 self-sizing cell 的過去、現在、與未來

estimatedItemSize

Page 47: iOS 上 self-sizing cell 的過去、現在、與未來

estimatedItemSize• iOS 8 above

• UICollectionViewFlowLayout

• estimatedItemSize

• UICollectionViewCell

• preferredLayoutAttributesFittingAttributes

Page 48: iOS 上 self-sizing cell 的過去、現在、與未來

estimatedItemSize

• UITableView estimatedRowHeight

• UICollectionViewFlowLayoutflowLayout.estimatedItemSize = CGSize.init(width: UIScreen.main.bounds.size.width / 2 - 20, height: 100)

Page 49: iOS 上 self-sizing cell 的過去、現在、與未來

Sample Codefunc preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { self.contentView.layoutIfNeeded() layoutAttributes.frame.size.height = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height layoutAttributes.frame.size.width = self.bounds.size.width

return layoutAttributes }

Page 50: iOS 上 self-sizing cell 的過去、現在、與未來

• class reference super.preferredLayoutAttributesFitting(layoutAttributes)

• newLayoutAttributes size 1000x1000

• iOS 10self.contentView.layoutIfNeeded()

Page 51: iOS 上 self-sizing cell 的過去、現在、與未來

• estimatedItemSize cellpreferredLayoutAttributesFitting

• estimatedItemSize sizeForItemAtIndexPath

Page 52: iOS 上 self-sizing cell 的過去、現在、與未來

preferredLayoutAttributesFittingAttributes

Page 53: iOS 上 self-sizing cell 的過去、現在、與未來

preferredLayoutAttributesFittingAttributes

• + ...

Page 54: iOS 上 self-sizing cell 的過去、現在、與未來

•constraint

• willDisplayCell

• ADKSetConstraintConstant:forAttribute: can help!

Page 55: iOS 上 self-sizing cell 的過去、現在、與未來

• cellestimatedItemSize

flowLayout

•collectionView:layout:sizeForItemAtIndexPath:

Page 56: iOS 上 self-sizing cell 的過去、現在、與未來

Expandable cell

• estimatedItemSize+invalidLayout+UIViewAnimation

• sizeForCellAtIndexPath+performBatchUpdate:

Page 57: iOS 上 self-sizing cell 的過去、現在、與未來

• TableView

• estimatedRowHeight

• CollectionView

• cell estimatedItemSize

• sizeForItemAtIndexPath

• Custom flowLayout

• AppDevKit might help for nib!

Page 58: iOS 上 self-sizing cell 的過去、現在、與未來

Xcode 8 & iOS 10

Page 59: iOS 上 self-sizing cell 的過去、現在、與未來

self-sizing in iOS 10

• WWDC 216 What's New in UICollectionView in iOS 10

flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize

Page 60: iOS 上 self-sizing cell 的過去、現在、與未來

xib in Xcode 8

Page 61: iOS 上 self-sizing cell 的過去、現在、與未來

xib in Xcode 8

• xib constraint frame

• init frame

• Update:Xcode 8.1

Page 62: iOS 上 self-sizing cell 的過去、現在、與未來

Frame size1000!

Page 63: iOS 上 self-sizing cell 的過去、現在、與未來

xib in Xcode 8

NSLayoutConstraint(item: star, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: self.frame.height).isActive = true;

” ”

Page 64: iOS 上 self-sizing cell 的過去、現在、與未來

1000x1000 star!

Page 65: iOS 上 self-sizing cell 的過去、現在、與未來

Solution

• or 0NSLayoutConstraint(item: star, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: 0.0).isActive = true;

Page 66: iOS 上 self-sizing cell 的過去、現在、與未來

xibXcode 7

Xcode 8

Page 67: iOS 上 self-sizing cell 的過去、現在、與未來

constraint...

Page 68: iOS 上 self-sizing cell 的過去、現在、與未來

• auto layout constraint

• contentView

layoutIfNeeded

• preferredMaxLayoutWidth

Page 69: iOS 上 self-sizing cell 的過去、現在、與未來
Page 70: iOS 上 self-sizing cell 的過去、現在、與未來

• xib

• size constraint

• constraint self.frame.size

Page 71: iOS 上 self-sizing cell 的過去、現在、與未來

constraint ...

Page 72: iOS 上 self-sizing cell 的過去、現在、與未來

Self-sizing checklist

• estimatedItemSize

• cellpreferredLayoutAttributesFittingAttributes

• self-sizing constraint

Page 73: iOS 上 self-sizing cell 的過去、現在、與未來

Self-sizing checklist• sizeForItemAtIndexPath:

• cache cell

• constraint frame

• self-sizing constraint

• ADKNibSizeCalculator & ADKCellDynamicSizeCalculator can help!

Page 74: iOS 上 self-sizing cell 的過去、現在、與未來

• self-sizing constraint

• ( )

• preferredMaxLayoutWidth

Page 75: iOS 上 self-sizing cell 的過去、現在、與未來

Thank you!