[UIKit] UIScrollView "Scrollable content size is ambiguous." ์—๋Ÿฌ ํ•ด๊ฒฐ
iOS/UIKit

[UIKit] UIScrollView "Scrollable content size is ambiguous." ์—๋Ÿฌ ํ•ด๊ฒฐ

 

์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์š”์ƒํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ์—ˆ๋Š”๋ฐ... ์กฐ์ž‘ํ•˜๋Š” ๋ฐ๋‚˜ ๋ณด์—ฌ์ง€๋Š” ๋ทฐ์—์„œ๋Š” ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์ง€๋งŒ, View Hierarchy๋ฅผ ๋ณด๋ฉด ์ œ์•ฝ์กฐ๊ฑด ์—๋Ÿฌ๊ฐ€ ์žˆ๋Š” ๊ฒŒ ๋„ˆ๋ฌด๋„ˆ๋ฌด ๊ฑฐ์Šฌ๋ ค์„œ ๊ฒฐ๊ตญ ํ•ด๊ฒฐํ–ˆ๋‹ค!

 

(ํ‰์†Œ scrollView ์—๋Ÿฌ๋Š” ํ„ฐ์น˜๋‚˜ ์Šคํฌ๋กค์ด ์•ˆ ๋˜๋Š” ๋“ฑ์˜ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์ง€๋งŒ, ์ด ๊ฒฝ์šฐ๋Š” ๋ชจ๋“  ์ธํ„ฐ๋ž™์…˜์ด ๋‹ค ๊ฐ€๋Šฅํ•˜๊ธด ํ–ˆ์Œ.) 
์›๋ž˜ ์ฝ”๋“œ (SnapKit ์‚ฌ์šฉ)
        self.view.addSubviews([scrollView])
        self.scrollView.addSubview(contentView)
        self.contentView.addSubviews([infoTitleLabel, profileInfoView, tagTitleLabel, tagCollectionView, emailTitleLabel, emailButton])
        
        self.scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.contentView.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.centerX.top.bottom.equalToSuperview()
        }
 
 
 
 
์—ฌ๋Ÿฌ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด ๋ณธ ๊ฒฐ๊ณผ... scrollView๋ฅผ ์ด์šฉํ•ด ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌํ˜„ํ•  ๋•Œ์˜ ๋‚˜์˜ ๋Œ€์ „์ œ(?)๋ฅผ ์„ธ์šธ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ

1๏ธโƒฃ scrollView๋Š” top, bottom, leading(left), trailing(right)๋ฅผ ๋ชจ๋‘ ์Šคํฌ๋กค ๋  ์˜์—ญ ์‚ฌ์ด์ฆˆ์— ๋งž์ถ˜๋‹ค. (๋ณดํ†ต equalToSuperView ์‚ฌ์šฉ)

self.scrollView.snp.makeConstraints { make in
    make.edges.equalToSuperview()
}

2๏ธโƒฃ scrollView ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” contentView๋Š” top, bottom, leading(left), trailing(right)๋Š” ๋ชจ๋‘ equalToSuperView๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ scrollView ์˜์—ญ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถ”๊ณ , ์„ธ๋กœ ์Šคํฌ๋กค์„ ์›ํ•˜๋ฉด width๋ฅผ equalToSuperView, ๊ฐ€๋กœ ์Šคํฌ๋กค์„ ์›ํ•˜๋ฉด height๋ฅผ equalToSuperView ํ•œ๋‹ค.

self.contentView.snp.makeConstraints { make in
    make.edges.width.equalToSuperview() // ์„ธ๋กœ ์Šคํฌ๋กค
}

4๏ธโƒฃ contentView์˜ height๋Š” ํ•ญ์ƒ ์ •ํ•ด์ ธ ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, height ๊ฐ’์„ ์ง์ ‘ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ , contentView์˜ ํ•˜์œ„ UI ์ปดํฌ๋„ŒํŠธ๋“ค์„ ํ†ตํ•ด height์„ ์„ค์ •ํ•ด๋„ ๋œ๋‹ค. (ํ•˜์œ„ UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•ด contentView์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๊ฒฐ์ •๋˜๋ฏ€๋กœ,,, (์„ธ๋กœ ์Šคํฌ๋กค์ด๋ฉด height๊ฐ€ ๊ฒฐ์ •๋จ) ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์˜ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ž˜ ๋„ฃ์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค!

self.infoTitleLabel.snp.makeConstraints { make in
    make.top.equalToSuperview().inset(26) // top ์„ค์ •!
    make.horizontalEdges.equalToSuperview().inset(20)
    make.height.equalTo(27) // height 1
}
self.profileInfoView.snp.makeConstraints { make in
    make.top.equalTo(self.infoTitleLabel.snp.bottom).offset(10)
    make.horizontalEdges.equalToSuperview().inset(20)
    make.height.equalTo(194) // height 2
    make.bottom.equalToSuperView().inset(40) // bottom ์„ค์ •!
}
 
 
 
ํ•ด๊ฒฐ!


์ „์ฒด ์ฝ”๋“œ
self.view.addSubviews([scrollView])
self.scrollView.addSubview(contentView)
self.contentView.addSubviews([infoTitleLabel, profileInfoView])

self.scrollView.snp.makeConstraints { make in
    make.edges.equalToSuperview()
}

self.contentView.snp.makeConstraints { make in
    make.edges.width.equalToSuperview()
}

self.infoTitleLabel.snp.makeConstraints { make in
    make.top.equalToSuperview().inset(26) // top ์„ค์ •!
    make.horizontalEdges.equalToSuperview().inset(20)
    make.height.equalTo(27) // height 1
}
self.profileInfoView.snp.makeConstraints { make in
    make.top.equalTo(self.infoTitleLabel.snp.bottom).offset(10)
    make.horizontalEdges.equalToSuperview().inset(20)
    make.height.equalTo(194) // height 2
    make.bottom.equalToSuperView().inset(40) // bottom ์„ค์ •!
}
 
 
~ ์•„๋‹ ์ˆ˜๋„ ์žˆ์Œ ~
๋ฐ˜์‘ํ˜•