Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's iOS Story/๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฌ๋“ค

[UIKit] ์™œ ์•ˆ๋– ? ๋งŒ๋“  View๊ฐ€ ์•ˆ๋– ์š”. (๋ ˆ์ด์•„์›ƒ์ด ๊นจ์งˆ๋•Œ ์ฝ˜์†”์ฐฝ ์‚ดํŽด๋ณด๋Š” URL ์ด ์žˆ๋‹ค?!)

๊ฐ์ž ๐Ÿฅ” 2022. 10. 20. 16:19
๋ฐ˜์‘ํ˜•

๐Ÿ“ฑ CollectionView๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ์™œ ์•ˆ๋– ?

Code base๋กœ ๊ฐœ๋ฐœํ•  ๋•Œ ํ•ญ์ƒ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ, (์•„์ง ๋ฏธ์ˆ™ํ•œ ๊ฐ์ž๋ผ์„œ ํ•ญ์ƒ,,,์ƒ๊ธฐ๊ณคํ•œ๋‹ค. ์–ธ์ œ์ฏค ๊ณ ์ˆ˜๊ฐ์ž๊ฐ€ ๋  ์ˆ˜ ์žˆ์„๊นŒ!)
View๊ฐ€ ์•ˆ๋œฐ๋•Œ ์ฝ˜์†”์ฐฝ์„ ์‚ดํŽด๋ด์•ผํ•œ๋‹ค๋Š” ์ ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. (์ฝ˜์†”์ฐฝ ์–ด๋ ต๊ฒŒ ์ƒ๊ฒผ์–ด,,)

์ง€๊ธˆ์€ CollectionView๋ฅผ ๋งŒ๋“ค๊ณ  ์žˆ๋Š”๋ฐ ๋ทฐ๊ฐ€ ์•ˆ๋“œ๊ณ  ์žˆ๋‹ค. hierarchy๋ฅผ ์‚ดํŽด๋ณด์•„๋„ ๋œจ์ง€ ์•Š๋Š”๋‹ค! (์•„์ดํŒจ๋“œ์ž„)

๋ณด์ด์ง€ ์•Š๋Š”,,, CollectionView

๋‚ด๊ฐ€ ์“ด collectionview์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

private func configureCollectionView() {
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(DynamicCollectionViewCell.self, forCellWithReuseIdentifier: DynamicCollectionViewCell.identifier)
}

private func setupLayout() {
    view.addSubview(collectionView)
    NSLayoutConstraint.activate([
        collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
    ])
}

collectionView์˜ top, leading, trailing, bottom ์„ ์žก์•„์ฃผ์—ˆ๊ณ  ์ด ๋ชจ๋‘ super view๋ฅผ ๊ธฐ์ค€์œผ๋กœ safeArea ์•ˆ์ชฝ์œผ๋กœ ๋“ค์–ด์˜ค๊ฒŒ ์žก์•„์ฃผ์—ˆ๋‹ค.

 

๐Ÿค” ๋„๋Œ€์ฒด ์–ด๋””์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์œผ๋‹ˆ, ์ฝ˜์†”์ฐฝ์„ ์‚ดํŽด๋ณด์ž.

์šฐ๋ฆฌ ํŒ€์›์˜ ์ฒœ์žฌ๊ฐ“๋“€๋‹˜์˜ ๋ง์”€์„ ๋“ค์–ด๋ณด๋ฉด, layout์ด ๊นจ์ง€๋ฉด '์ฝ˜์†”์ฐฝ์—์„œ ํ™”๋ฅผ๋‚ธ๋‹ค'๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฝ˜์†”์ฐฝ์„ ์‚ดํŽด๋ณด์•˜๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ๊ตฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ฝ˜์†”์ฐฝ์—์„œ๋Š”  ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆ˜๋งŽ์€ ์—๋Ÿฌ๋“ค์ด ํ™”๋ฅผ ๋‚ด๊ณ  ์žˆ์—ˆ๋‹ค.

์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ํ•œ,,, 10๊ฐœ์ •๋„? ํ•˜ํ•˜ํ•˜ํ•˜ 

๋ ˆ์ด์•„์›ƒ์ด ๊นจ์งˆ๋•Œ ์–ด๋–ป๊ฒŒ ์•Œ์•„๋ด์•ผํ•˜๋Š”์ง€ ์ฐพ์•„๋ณด๋‹ค๊ฐ€, ์•„๋ž˜์™€ ๊ฐ™์€ ์‚ฌ์ดํŠธ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

https://www.wtfautolayout.com/

 

WTF Auto Layout?

Make sense of cryptic Auto Layout error logs.

www.wtfautolayout.com

์ด ํŽ˜์ด์ง€์—์„œ๋Š” ์–ด๋Š๋ถ€๋ถ„์—์„œ ์–ด๋–ค ๋ ˆ์ด์•„์›ƒ์ด ๊นจ์กŒ๋Š”์ง€ ์กฐ๊ธˆ์ด๋‚˜๋งˆ ์‹œ๊ฐ์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์—ฌ๊ธฐ๋‹ค๊ฐ€๋Š” ์ฝ˜์†”์ฐฝ์— ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ์ด ๊ฐœ์ง€๋Š” ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜๋ฉด ๋œ๋‹ค. 

"<NSAutoresizingMaskLayoutConstraint:0x600002ee3a70 h=--& v=--& UICollectionView:0x143811800.width == 0   (active)>",
"<NSLayoutConstraint:0x600002ef0c30 UICollectionView:0x143811800.leading == UILayoutGuide:0x6000034d1340'UIViewSafeAreaLayoutGuide'.leading   (active)>",
"<NSLayoutConstraint:0x600002ef0a50 UICollectionView:0x143811800.trailing == UILayoutGuide:0x6000034d1340'UIViewSafeAreaLayoutGuide'.trailing   (active)>",
"<NSLayoutConstraint:0x600002eea580 'UIView-Encapsulated-Layout-Width' UIView:0x141709c50.width == 744   (active)>",
"<NSLayoutConstraint:0x600002ef0370 'UIViewSafeAreaLayoutGuide-left' H:|-(0)-[UILayoutGuide:0x6000034d1340'UIViewSafeAreaLayoutGuide'](LTR)   (active, names: '|':UIView:0x141709c50 )>",
"<NSLayoutConstraint:0x600002ef05a0 'UIViewSafeAreaLayoutGuide-right' H:[UILayoutGuide:0x6000034d1340'UIViewSafeAreaLayoutGuide']-(0)-|(LTR)   (active, names: '|':UIView:0x141709c50 )>"

์ด ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•ด์„œ, Go! ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋œฌ๋‹ค!

 

์ด ํŽ˜์ด์ง€์—์„œ ์–ด๋””์„œ autolayout ์—๋Ÿฌ๊ฐ€๋‚˜๋Š”์ง€ ์‚ดํŽด๋ณด์•˜๋‹ค. ์ด๊ฑธ๋กœ ๋‘์‹œ๊ฐ„์„ ์‚ฝ์งˆํ–ˆ๋Š”๋ฐ... ๊ฒฐ๊ณผ๋Š”

๋ญ ์ด๊ฒƒ์ €๊ฒƒ ์•ˆ๋งž๊ธด ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰์— ์ดˆ๋ก์ค„ ์ณ๋†“์€ ๊ฒƒ์„ ๋ณด์ž. ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ Autolayout ์œผ๋กœ ๋ช‡์‹œ๊ฐ„์„ ์ง€๊ธˆ ์”จ๋ฆ„ํ–ˆ๋Š”๋ฐ,,,์ €๊ฑธ ์•ˆ์จ์„œ์˜€๋‹ค... ใ…‹ใ…‹ใ…‹ใ…‹ ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ ํ–‰๋ณดํ•˜๊ตฐ

collectionView.translatesAutoresizingMaskIntoConstraints = false

์ด๊ฑธ ์“ฐ์ง€ ์•Š์•„์„œ ๊ทธ๋ ‡๋‹ค. ์œผํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ ์•ž์œผ๋กœ ์ž˜ ์ƒ๊ฐํ•˜์ž,, 

 

์œ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋Œ๋ ค๋ณด๋‹ˆ, ์•„๋ž˜์™€ ๊ฐ™์ด ๋ทฐ๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค. (ํ•„์š”์— ์˜ํ•ด ipad๋กœ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์Šด๋‹ˆ๋‹ค ํ•˜ํ•ฌ)

์ด๋ ‡๊ฒŒ ๋œจ๋Š”๋ฐ, ์–ด์ฐŒ๋๋“  ๋‚ด๊ฐ€ cell์—๋Œ€ํ•œ ๋ ˆ์ด์•„์›ƒ์„ ์ž˜๋ชป ์žก์€๊ฒƒ์€ ๋งž๋Š”๊ฒƒ๊ฐ™๋‹ค. ๋‹ค์‹œ ์žก์•„๋ณด์ž!

 

๐Ÿ˜Š ๊ฒฐ๋ก 

collectionView.translatesAutoresizingMaskIntoConstraints = false

๊นŒ๋จน์ง€ ๋ง๊ณ  ์ž‘์„ฑํ•˜์ž! 

๋ฐ˜์‘ํ˜•