๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/Library] Swift Snapkit์œผ๋กœ ์ฝ”๋“œ๋กœ ์˜คํ† ๋ ˆ์ด์•„์›ƒ ์‰ฝ๊ฒŒํ•˜๊ธฐ(Easy Programmatically AutoLayout)

by Fomagran ๐Ÿ’ป 2020. 4. 10.
728x90
๋ฐ˜์‘ํ˜•

Snapkit์€ ์ฝ”๋“œ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 

์šฐ์„  ํŒŸํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด 'SnapKit', '~> 5.0.0'์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. ๋ฒ„์ „์€ ๋ฐ”๋€” ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜ ์Šค๋ƒ…ํ‚ท ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ฐ€์…”์„œ ํ™•์ธํ•˜์‹œ๊ณ  ์ถ”๊ฐ€ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

https://github.com/SnapKit/SnapKit

 

SnapKit/SnapKit

A Swift Autolayout DSL for iOS & OS X. Contribute to SnapKit/SnapKit development by creating an account on GitHub.

github.com

 

๊ทธ๋ฆฌ๊ณ  ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ด๋™ํ•ด 'SnapKit'์„ importํ•ด์ฃผ๊ณ 

๋ฌด์ง€๊ฐœ์ƒ‰ UIView๋“ค์„ ๋งŒ๋“ค์–ด๋†“์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณค view์— addSubview๋กœ ๋ฌด์ง€๊ฐœ์ƒ‰ ๋ทฐ๋“ค์„ ๋„ฃ์–ด์ฃผ๊ณ , ๊ฐ ๊ฐ ๋ทฐ์˜ ์ด๋ฆ„์— ๋งž๊ฒŒ ์ƒ‰๋„ backgroundColor๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

์ž ๋จผ์ € ๋นจ์ฃผ๋…ธ ๋ทฐ๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด redView.snp.makeConstraints { make in  <-๋Š” ๋นจ๊ฐ„์ƒ‰๋ทฐ์— ์ฝ˜์ŠคํŠธ๋ ˆ์ธํŠธ ๋งŒ๋“ค๊ฑฐ์•ผ

make.top.equalTo(view.snp.top) <- ์œ„๋ฅผ ํ˜„์žฌ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์˜ view์˜ ์œ„๋ž‘ ๋˜‘๊ฐ™์ด ํ•ด์ค˜!

make.size.width.height.equalTo <- ๋ทฐํฌ๊ธฐ๋ฅผ ๊ฐ€๋กœ 100 ์„ธ๋กœ 100์œผ๋กœ ๋งŒ๋“ค์–ด์ค˜

make.left.equalTo(view.snp.left) <- ์™ผ์ชฝ๋„ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์˜ view์˜ ์™ผ์ชฝ์ด๋ž‘ ๋˜‘๊ฐ™์ด ํ•ด์ค˜!

์ด๋Ÿฐ์‹์œผ๋กœ ์˜ค๋ Œ์ง€์ƒ‰๋ทฐ๋Š” ๋นจ๊ฐ„์ƒ‰๋ทฐ ์•„๋ž˜์— ๋…ธ๋ž€์ƒ‰๋ทฐ๋Š” ์˜ค๋ Œ์ง€์ƒ‰ ์•„๋ž˜๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณค ์ดˆ๋ก์ƒ‰,ํŒŒ๋ž‘์ƒ‰,๋‚จ์ƒ‰ ๋ทฐ๋“ค์€ ๋…ธ๋ž€์ƒ‰๋ทฐ์™€ ๋˜‘๊ฐ™์€ ๋†’์ด์—์„œ ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ๋Š”

์•„๋ž˜์™€ ๊ฐ™์ด make.top.equalTo(yellow.snp.top) <- ๋…ธ๋ž€์ƒ‰๋ทฐ๋ž‘ ์œ„๋ฅผ ๋˜‘๊ฐ™์ด ํ•ด์ค˜

make.size.width.height.equalTo <- ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ

make.left.equalTo(yellowView.snp.right) <- ๋‚ด ์™ผ์ชฝ์„ ๋…ธ๋ž€์ƒ‰๋ทฐ ์˜ค๋ฅธ์ชฝ์— ๋ถ™์—ฌ์ค˜

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŒŒ๋ž‘์ƒ‰๋ทฐ๋Š” ์ดˆ๋ก์ƒ‰๋ทฐ ์˜ค๋ฅธ์ชฝ์˜†์— ๋‚จ์ƒ‰๋ทฐ๋Š” ์ดˆ๋ก์ƒ‰ ๋ทฐ ์˜ค๋ฅธ์ชฝ์˜†์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

์•„๋ž˜ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค

๊ทธ๋ฆฌ๊ณค ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณด๋ผ์ƒ‰๋ทฐ๋Š” ๋‹ค ํ•„์š”์—†๊ณ  ๊ทธ๋ƒฅ ๊ฐ€์šด๋ฐ๋‹ค ๋†”์ค˜๋Š”

make.center.equalTo(view.snp.center) <- ํ˜„์žฌ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์˜ ๊ฐ€์šด๋ฐ๋ž‘ ๋ณด๋ผ์ƒ‰๋ทฐ ๊ฐ€์šด๋ฐ๋ฅผ ๋˜‘๊ฐ™์ด ํ•ด์ค˜ 

์•„๋ž˜ํ™”๋ฉด๊ณผ ๊ฐ™์ด ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

์ „์ฒด์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import UIKit
import SnapKit
 
 
@available(iOS 13.0*)
class TabbarViewController1: UIViewController,UITextFieldDelegate {
    
    var redView = UIView()
    var orangeView = UIView()
    var yellowView = UIView()
    var greenView = UIView()
    var blueView = UIView()
    var indigoView = UIView()
    var purpleView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(redView)
        self.view.addSubview(orangeView)
        self.view.addSubview(yellowView)
        self.view.addSubview(greenView)
        self.view.addSubview(blueView)
        self.view.addSubview(indigoView)
        self.view.addSubview(purpleView)
        
        redView.backgroundColor = .red
        orangeView.backgroundColor = .orange
        yellowView.backgroundColor = .yellow
        greenView.backgroundColor = .green
        blueView.backgroundColor = .blue
        indigoView.backgroundColor = .systemIndigo
        purpleView.backgroundColor = .purple
        
        redView.snp.makeConstraints { make in
            make.top.equalTo(view.snp.top)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(view.snp.left)
        }
        orangeView.snp.makeConstraints { make in
            make.top.equalTo(redView.snp.bottom)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(redView.snp.left)
        }
        yellowView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        greenView.snp.makeConstraints { make in
            make.top.equalTo(yellowView.snp.top)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        blueView.snp.makeConstraints { make in
            make.top.equalTo(greenView.snp.top)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        indigoView.snp.makeConstraints { make in
            make.top.equalTo(blueView.snp.top)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
 
        purpleView.snp.makeConstraints { make in
            make.center.equalTo(view.snp.center)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
    }
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋” ์ž์„ธํ•œ ๊ฑด SnapKit ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊นƒํ—ˆ๋ธŒ ์ฃผ์†Œ๋กœ ๊ฐ€์…”์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์œ„์— ์ฒจ๋ถ€ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค~

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€