SlideShare a Scribd company logo
iOS self-sizing cell
(Jeff Lin)
• Yahoo AppDevKit -
- (Anistar)
• https://github.com/yahoo/AppDevKit
• – iOS BDD - (qcl)
• https://github.com/qcl/zizhi
About me
• YAHOO App Engineer
• iOS App
• AppDevKit
Agenda
• Self-sizing
• Self-sizing
• Xcode 8
Self-sizing
iOS 上 self-sizing cell 的過去、現在、與未來
iOS 上 self-sizing cell 的過去、現在、與未來
/
/
Expandable cell
• iOS dynamic type
• UIFont.preferredFontForTex
tStyle:
iOS 上 self-sizing cell 的過去、現在、與未來
•
• UIImageView, UILabel, UITextView…
• Container
• UITableView, UICollectionView
•
• storyboard, xib, constraint
Layout
Self-sizing ?
!
• auto self-sizing
• NSString
• sizeWithAttributes
• boundingRectWithSize:options:attributes:context:
• UILabel
• sizeToFit:
• UIScrollView/UITextView
• contentSize
• size ceil()
• sizeToFit: frame
constraint
UITextView contentSize
• sizeForItemAtIndexPath:
• textView.ContentSize
iOS ?
UITableview self sizing
• UILabel numberOfLines = 0
• AutoLayout
self.tableView?.rowHeight = UITableViewAutomaticDimension
self.tableView?.estimatedRowHeight = 100
That’s all!
UITableview self sizing
•
• layout
UICollectionView
• Grid view,
• /
layout
•
UICollectionView self-sizing
AppDevKit
• UICollectionViewCell
• subclass ADKTableViewDynamicSizeCell/
ADKCollectionViewDynamicSizeCell
• constraint
• collectionView:layout:sizeForItemAtIndexPath:
• ADKNibCacheManager cell
• cell
AppDevKit Solution
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
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()
AppDevKit Solution
4.
func setup(cell: SSSmallCardCollectionViewCell,
indexPath: IndexPath) {
let productInfo = self.products[indexPath.row]
cell.contentView.layoutIfNeeded()
cell.titleLabel?.text = productInfo.productTitle
...
}
AppDevKit Solution
5.
let size =
ADKCellDynamicSizeCalculator.sharedInstance().size
(forDynamicHeightCellInstance: cell,
preferredSize: preferSize)
return size
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
}
Recap
1. cell
2.
3. layoutIfNeeded
constraint
4.
5.
ImageView
Title
Description
Price
Recap
1. cell
2.
3. layoutIfNeeded
constraint
4.
5.
ImageView
Title
Description
Price
Recap
1. cell
2.
3. layoutIfNeeded
constraint
4.
5.
ImageView
Title
Description
Price
Recap
1. cell
2.
3. layoutIfNeeded
constraint
4.
5.
ImageView
Title
Description
Price
ImageView
SO NICE
$ 790
ImageView
SO NICE
790
Recap
1. cell
2.
3. layoutIfNeeded
constraint
4.
5.
ImageView
SO NICE
$ 790
SO NICE
,
,
AppDevKit Solution
ADKCellDynamicSizeCalculator.sharedInsta
nce().size(forDynamicHeightCellInstance:
cell, preferredSize: CGSize.init(width:
collectionView.frame.size.width / 2.0
- 10, height: 0.0))
10px
How AppDevKit works?
•
CGSize resultSize = [contentView
systemLayoutSizeFittingSize:UILayoutFittingCompr
essedSize];
resultSize.width = preferredSize.width;
resultSize.height = MAX(resultSize.height,
preferredSize.height);
systemLayoutSizeFittingSize
• UILayoutFittingCompressedSize
• view constraints size
• UILayoutFittingExpandedSize
• view constraints size
layoutIfNeeds
• layoutIfNeeds
• reloadData contentSize
• Animate autoLayout
contentSize
// contentSize: {0, 0}
collectionView.dataSource = self.delegator
// contentSize: {0, 0}
collectionView.reloadData()
// contentSize: {0, 0}
collectionView.layoutIfNeeded
// contentSize: {375, 2557.5}
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
iOS ?
cell
• collectionView:layout:sizeForItemAtIndexPath:
• Customized UICollectionViewLayout
estimatedItemSize
estimatedItemSize
• iOS 8 above
• UICollectionViewFlowLayout
• estimatedItemSize
• UICollectionViewCell
• preferredLayoutAttributesFittingAttributes
estimatedItemSize
• UITableView estimatedRowHeight
• UICollectionViewFlowLayout
flowLayout.estimatedItemSize = CGSize.init(width:
UIScreen.main.bounds.size.width / 2 - 20, height:
100)
Sample Code
func preferredLayoutAttributesFitting(_
layoutAttributes: UICollectionViewLayoutAttributes)
-> UICollectionViewLayoutAttributes {
self.contentView.layoutIfNeeded()
layoutAttributes.frame.size.height =
systemLayoutSizeFitting(UILayoutFittingCompressedSi
ze).height
layoutAttributes.frame.size.width =
self.bounds.size.width
return layoutAttributes
}
• class reference
super.preferredLayoutAttributesFitting(layoutAttributes)
• newLayoutAttributes size 1000x1000
• iOS 10
self.contentView.layoutIfNeeded()
• estimatedItemSize cell
preferredLayoutAttributesFitting
• estimatedItemSize sizeForItemAtIndexPath
preferredLayoutAttributesFittingA
ttributes
preferredLayoutAttributesFittingA
ttributes
•
•
• + ...
•
constraint
• willDisplayCell
• ADKSetConstraintConsta
nt:forAttribute: can help!
• cell
estimatedItemSize
flowLayout
•
collectionView:layout:sizeFo
rItemAtIndexPath:
Expandable cell
•
• estimatedItemSize+invalidLayout
+UIViewAnimation
• sizeForCellAtIndexPath+performBatchUpdate:
• TableView
• estimatedRowHeight
• CollectionView
• cell estimatedItemSize
• sizeForItemAtIndexPath
• Custom flowLayout
• AppDevKit might help for nib!
Xcode 8 & iOS 10
self-sizing in iOS 10
• WWDC 216 What's New in UICollectionView in
iOS 10
•
flowLayout.estimatedItemSize =
UICollectionViewFlowLayoutAutomaticSize
xib in Xcode 8
xib in Xcode 8
• xib constraint frame
• init frame
• Update:Xcode 8.1
Frame size1000!
xib in Xcode 8
NSLayoutConstraint(item: star,
attribute: .height, relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 0.0, constant:
self.frame.height).isActive = true;
” ”
1000x1000 star!
Solution
• or 0
NSLayoutConstraint(item: star,
attribute: .height, relatedBy: .equal, toItem:
self, attribute: .height, multiplier: 1.0,
constant: 0.0).isActive = true;
xib
Xcode 7
Xcode 8
constraint...
• auto layout constraint
• contentView
layoutIfNeeded
• preferredMaxLayoutWidth
iOS 上 self-sizing cell 的過去、現在、與未來
• xib
• size constraint
• constraint self.frame.size
constraint ...
Self-sizing checklist
• estimatedItemSize
• cell
preferredLayoutAttributesFittingAttributes
• self-sizing constraint
Self-sizing checklist
• sizeForItemAtIndexPath:
• cache cell
• constraint frame
• self-sizing constraint
• ADKNibSizeCalculator & ADKCellDynamicSizeCalculator can
help!
…
• self-sizing constraint
• ( )
• preferredMaxLayoutWidth
Thank you!

More Related Content

PDF
SwiftUI and Combine All the Things
PDF
MVVM with SwiftUI and Combine
PDF
Maste Thesis Ap Thiago Assis
PDF
HBase Sizing Notes
PDF
Hi performance table views with QuartzCore and CoreText
PDF
Introducing collection views - Mark Pospesel
PDF
Auto Layout part 1
PPTX
Getting started with titanium
SwiftUI and Combine All the Things
MVVM with SwiftUI and Combine
Maste Thesis Ap Thiago Assis
HBase Sizing Notes
Hi performance table views with QuartzCore and CoreText
Introducing collection views - Mark Pospesel
Auto Layout part 1
Getting started with titanium

Similar to iOS 上 self-sizing cell 的過去、現在、與未來 (20)

PPTX
Getting started with Appcelerator Titanium
PDF
Mongo db washington dc 2014
PDF
初心者向けGAE/Java説明資料
PPTX
Creating personalized cross platform mobile apps with the Sitecore Mobile SDK
PPT
iOS Programming 101
PDF
Android
PPTX
Intuit Mobile Custom Views
PPTX
Swift Tableview iOS App Development
PPT
Programming iOS in C#
PPTX
Creating Dynamic Sidebars & Widgets in WordPress
PDF
Xamarin 9/10 San Diego Meetup
PDF
iOS 7 SDK特訓班
PPTX
Ext JS Introduction
PPTX
Untangling spring week5
PDF
Fragments: Why, How, What For?
PPTX
Work With Images
PPTX
Head first iOS programming
PPTX
Collection view layout
PDF
Engineering the New LinkedIn Profile
ODP
CreateJS hackathon in Zurich
Getting started with Appcelerator Titanium
Mongo db washington dc 2014
初心者向けGAE/Java説明資料
Creating personalized cross platform mobile apps with the Sitecore Mobile SDK
iOS Programming 101
Android
Intuit Mobile Custom Views
Swift Tableview iOS App Development
Programming iOS in C#
Creating Dynamic Sidebars & Widgets in WordPress
Xamarin 9/10 San Diego Meetup
iOS 7 SDK特訓班
Ext JS Introduction
Untangling spring week5
Fragments: Why, How, What For?
Work With Images
Head first iOS programming
Collection view layout
Engineering the New LinkedIn Profile
CreateJS hackathon in Zurich
Ad

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Project quality management in manufacturing
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PPT on Performance Review to get promotions
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
web development for engineering and engineering
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
OOP with Java - Java Introduction (Basics)
Foundation to blockchain - A guide to Blockchain Tech
Project quality management in manufacturing
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT on Performance Review to get promotions
additive manufacturing of ss316l using mig welding
Lecture Notes Electrical Wiring System Components
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
R24 SURVEYING LAB MANUAL for civil enggi
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mechanical Engineering MATERIALS Selection
web development for engineering and engineering
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
OOP with Java - Java Introduction (Basics)
Ad

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