Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
使い勝手のよいCustomViewをつくる
Search
himaratsu
October 27, 2016
Technology
0
600
使い勝手のよいCustomViewをつくる
@IBDesignable & @IBInspectable が使えて、使い勝手のよいCustomViewの作り方をまとめて紹介しました。
iOS_LT #23 で発表した資料です。
himaratsu
October 27, 2016
Tweet
Share
More Decks by himaratsu
See All by himaratsu
Next.js × microCMSで道の駅サイトを作った話
himaratsu
0
750
PayPayフリマの速度改善
himaratsu
2
320
Goodbye Code Review, Hello Pair Programming
himaratsu
0
150
UICollectionViewでインタラクティブなCellの並び替え
himaratsu
2
9.7k
Other Decks in Technology
See All in Technology
サイバーエージェントにおける生成AIのリスキリング施策の取り組み / cyber-ai-reskilling
cyberagentdevelopers
PRO
2
200
国土交通省 データコンペ参加者向け勉強会
takehikohashimoto
0
150
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
490
AWS CDKでデータリストアの運用、どのように設計する?~Aurora・EFSの実践事例を紹介~/aws-cdk-data-restore-aurora-efs
mhrtech
4
680
AWS CodePipelineでコンテナアプリをデプロイした際に、古いイメージを自動で削除する
smt7174
1
120
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
480
Autify Company Deck
autifyhq
1
39k
わたしとトラックポイント / TrackPoint tips
masahirokawahara
1
240
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
750
来年もre:Invent2024 に行きたいあなたへ - “集中”と“つながり”で楽しむ -
ny7760
0
480
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
Featured
See All Featured
KATA
mclloyd
29
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Designing for humans not robots
tammielis
249
25k
GitHub's CSS Performance
jonrohan
1030
460k
Speed Design
sergeychernyshev
24
570
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Ruby is Unlike a Banana
tanoku
96
11k
Building Applications with DynamoDB
mza
90
6.1k
How to Ace a Technical Interview
jacobian
275
23k
Code Reviewing Like a Champion
maltzj
519
39k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
Transcript
͍উखͷΑ͍ CustomViewΛͭ͘Δ @himara2 / iOS_LT
CustomView? ։ൃ͍ͯ͠ΔͱɺطଘͷUIViewΛ֦ுͨ͠ಠࣗͷΫϥεΛ࡞Γͨ͘ͳΔ
͍উखͷΑ͍CustomView?
- Storyboard / xib Ͱ͑Δ ͍উखͷΑ͍CustomView?
- Storyboard / xib Ͱ͑Δ ͍উखͷΑ͍CustomView? - ιʔείʔυ͔Β͑Δ let customView
= MyCustomView(frame: CGRect(x: 50, y: 100, width: 270, height: 200)) customView.titleLabel.text = "Hello!" view.addSubview(customView) ͜Μͳײ͡ͰॳظԽͯ͑͠Δ
- Storyboard / xib Ͱ͑Δ ͍উखͷΑ͍CustomView? - ιʔείʔυ͔Β͑Δ - @IBDesignable
& @IBInspectable ʹରԠ
- Storyboard / xib Ͱ͑Δ ͍উखͷΑ͍CustomView? - ιʔείʔυ͔Β͑Δ - @IBDesignable
& @IBInspectable ʹରԠ - ৭ʑͳը໘͔Β͍͍͢
- Storyboard / xib Ͱ͑Δ ͍উखͷΑ͍CustomView? - ιʔείʔυ͔Β͑Δ - @IBDesignable
& @IBInspectable ʹରԠ - ৭ʑͳը໘͔Β͍͍͢ → ͻͱඞཁͳͷͰɺखॱΛ·ͱΊ·ͨ͠
Step.1 UIViewΛܧঝͨ͠ΫϥεΛ࡞ͯ͠ɺ xibϑΝΠϧʹίϯϙʔωϯτΛฒΔ
Step.2 xibͷʮFile’s Ownerʯͷ߲ʹɺ ࣗͰͭͬͨ͘ΫϥεΛͯΔ ※ʮViewʯʹͯͳ͍ͷ͕ϙΠϯτ
Step.3 ॳظԽͷίʔυΛॻ͘ override init(frame: CGRect) { super.init(frame: frame) commonInit() }
required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } private func commonInit() { let nib = UINib(nibName: "MyCustomView", bundle: nil) let view = nib.instantiate(withOwner: self, options: nil).first as! UIView addSubview(view) view.translatesAutoresizingMaskIntoConstraints = false let bindings = ["view": view] addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views: bindings)) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views: bindings)) }
Step.3 override init(frame: CGRect) { super.init(frame: frame) commonInit() } required
init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } private func commonInit() { let nib = UINib(nibName: "MyCustomView", bundle: nil) let view = nib.instantiate(withOwner: self, options: nil).first as! UIView addSubview(view) view.translatesAutoresizingMaskIntoConstraints = false let bindings = ["view": view] addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views: bindings)) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views: bindings)) } ॳظԽͷίʔυΛॻ͘ ίʔυ͔ΒCustomViewΛ ॳظԽͨ͠ͱ͖ݺΕΔ Storyboard/xib͔ΒCustomViewΛ ॳظԽͨ͠ͱ͖ݺΕΔ viewͷॳظԽॲཧ
Step.4 @IBDesignable, @IBInspectable ͷଐੑΛઃఆ @IBDesignable class MyCustomView: UIView { …
} @IBInspectable var titleText: String = "" { didSet { titleLabel.text = titleText } } @IBInspectable var iconImage: UIImage? { didSet { iconImageView.image = iconImage } }
!
tips ຖճviewͷॳظԽ·ΘΓͷίʔυΛॻ͘ͷπϥΠͷͰɺProtocolΛ༻ҙ͢Δͱྑ͍ protocol XibInstantiatable { func instantiate() } extension XibInstantiatable
where Self: UIView { func instantiate() { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle) guard let view = nib.instantiate(withOwner: self, options: nil).first as? UIView else { return } addSubview(view) view.translatesAutoresizingMaskIntoConstraints = false let bindings = ["view": view] addConstraints(NSLayoutConstraint.constraints(withVisualFormat: “H:|[view]|", … addConstraints(NSLayoutConstraint.constraints(withVisualFormat: “V:|[view]|", … } } override init(frame: CGRect) { super.init(frame: frame) instantiate() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) instantiate() } CustomViewଆ͔Β ͜Μͳײ͡Ͱ͏
NibDesignable ͱ͍͏ OSS ͱ͋Θͤͯ͏ͱศརʢΒ͍͠ʣ tips - Swift 3Ͱ͏·͘ϏϧυͰ͖ͣɺ·ͩࢼͤͯͳ͍ - ઌ΄ͲͷExtensionΛݞΘΓͯ͘͠ΕΔΑ͏ͳͷ
IUUQTHJUIVCDPNNCPHI/JC%FTJHOBCMF
ຊ͓ݟͤͨ͠αϯϓϧίʔυ ɹ- https://github.com/himaratsu/CustomViewSample ɹ- ࠓͷLTΛ͖͔͚ͬʹSwift 3ରԠ͠·ͨ͠✌ ੲॻ͍ͨϒϩά ɹ- http://himaratsu.hatenablog.com/entry/ios/customview ɹ-
ࠓͷΑΓ͏ͪΐͬͱৄ͘͠ॻ͍ͯ·͢ ͦͷଞ
͓ΘΓ