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
770
PayPayフリマの速度改善
himaratsu
2
320
Goodbye Code Review, Hello Pair Programming
himaratsu
0
160
UICollectionViewでインタラクティブなCellの並び替え
himaratsu
2
9.7k
Other Decks in Technology
See All in Technology
The Rise of LLMOps
asei
9
1.8k
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
LINEヤフーにおけるPrerender技術の導入とその効果
narirou
1
250
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
160
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
130
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
720
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
430
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
190
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
250
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
120
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.9k
Featured
See All Featured
Happy Clients
brianwarren
98
6.7k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Become a Pro
speakerdeck
PRO
25
5k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Embracing the Ebb and Flow
colly
84
4.5k
Done Done
chrislema
181
16k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Being A Developer After 40
akosma
87
590k
Statistics for Hackers
jakevdp
796
220k
Writing Fast Ruby
sferik
627
61k
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 ɹ-
ࠓͷΑΓ͏ͪΐͬͱৄ͘͠ॻ͍ͯ·͢ ͦͷଞ
͓ΘΓ