๐ŸŒฑ SeSAC

[SeSAC] 220809 TIL

taeeekki 2022. 8. 12. 18:26

IBInspectable, IBDesignable

์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ž‘์—…์„ ํ•˜๋‹ค๋ณด๋ฉด ์ธ์ŠคํŽ™ํ„ฐ์—์„œ ์†์„ฑ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ์ผ์ด ๋งŽ์€๋ฐ ํ•˜๋‚˜ ๊ฐ€์žฅ ๊ท€์ฐฎ์•˜๋˜ ๊ฒƒ์€ border ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒŒ ๊ธฐ๋ณธ์ ์œผ๋กœ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. border ๊ฐ’์„ ๋งค๋ฒˆ ์ฝ”๋“œ๋กœ ๋ช‡ ์ค„์”ฉ ์งœ๋Š” ๊ฒƒ๋ณด๋‹ค ์ธ์ŠคํŽ™ํ„ฐ ์ฐฝ์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์–ผ๋งˆ๋‚˜ ์ข‹์„๊นŒ?

 

IBInspectable๊ณผ IBDesignable ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ทธ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐ์ฒด์— ์†์„ฑ์„ ์ ์šฉํ•˜๊ณ  ํ™•์ธ ๊ฐ€๋Šฅ
  • ๋Ÿฐํƒ€์ž„์ด ์•„๋‹ˆ๋ผ ์ปดํŒŒ์ผ ํƒ€์ž„์— ํ™•์ธ ๊ฐ€๋Šฅ
  • ๋ฐ”๋กœ ๋ฐ˜์˜์ด ์•ˆ ๋  ์ˆ˜ ์žˆ์Œ
// ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋” ์ธ์ŠคํŽ™ํ„ฐ ์˜์—ญ Show
@IBInspectable
var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
    }
}

@IBInspectable
var borderWidth: CGFloat {
    get {
        return layer.borderWidth
    }
    set {
        layer.borderWidth = newValue
    }
}

@IBInspectable
var borderColor: UIColor {
    get {
        return UIColor(cgColor: layer.borderColor!)
    }
    set {
        layer.borderColor = newValue.cgColor
    }
}

 


 

Custom View Design(with XIB)

  1. XIB ํŒŒ์ผ๊ณผ UIView ํด๋ž˜์Šค ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
  2. File's Owner์— ํด๋ž˜์Šค ์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ
  3. Safe Area LayoutGuide ํ•ด์ œํ•˜๊ธฐ
  4. ์‚ฌ์ด์ฆˆ Freeform์œผ๋กœ ์กฐ์ ˆํ•˜๊ธฐ

 

required init?(coder: NSCoder) ์ด๋‹ˆ์…œ๋ผ์ด์ €(์ค‘์š”)

  • Xib ํŒŒ์ผ์€ ์ปดํŒŒ์ผ ์‹œ Nib ํŒŒ์ผ๋กœ ๋ณ€ํ™˜
  • ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” init(coder: NSCoder)๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•จ. (์ฝ”๋“œ ๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๋ผ Xib๋กœ ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค๋ฉด!)

 

override init(frame: CGRect)

  • ์ฝ”๋“œ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ
  • ์ฝ”๋“œ ๋ฒ ์ด์Šค์™€ Xib ๋ฒ ์ด์Šค๋Š” ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์„ ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ๊ฐ€์ ธ๊ฐ„๋‹ค.
  • ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ฝ”๋“œ ๋ฒ ์ด์Šค๋ผ๊ณ  ํ•˜๋”๋ผ๋„ required init?(coder: NSCoder) ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

 

Nib ํŒŒ์ผ ๋กœ๋“œ

  • Nib ํ˜•์‹์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ํ˜•์‹
  • Nib์„ ๋กœ๋“œํ•ด์„œ ์‚ฌ์šฉ์ž์˜ ํ™”๋ฉด์— ๋„ฃ์–ด์ฃผ๋Š” ์ž‘์—… ํ•„์š”
  • UINib์€ nib ์ปจํ…์ธ ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์บ์‹œํ•˜๊ณ  ์žˆ์Œ.
  • instantiate ์‹œ์ ์— Unarchving ํ•จ (- ์•„์นด์ด๋น™, ์–ธ์•„์นด์ด๋น™ ๊ฐœ๋… ์ •๋ฆฌ ํ•„์š”)
let view = UINib(nibName: "CardView", bundle: nil)
    .instantiate(withOwner: self).first as! UIView
view.frame = bounds
self.addSubview(view)

 


 

์…€ ์ค‘์ฒฉ ๊ตฌ์กฐ ์‹ค์Šต

๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ์ž…์žฅ์—์„œ ์ •๋ง ๊นŒ๋‹ค๋กœ์šด ๊ฒƒ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ค‘์ฒฉ ๊ตฌ์กฐ๊ฐ€ ์•„๋‹๊นŒ ์‹ถ๋‹ค. ์†”์งํžˆ iOS 13 ์ด์ƒ ๋ถ€ํ„ฐ๋Š” Compositional Layout ๋•๋ถ„์— ์ค‘์ฒฉ ๊ตฌ์กฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์ด ์‰ฌ์›Œ์กŒ๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•๋„ ์•Œ ํ•„์š”๊ฐ€ ์žˆ๋‹ค๊ณ  ๋ณธ๋‹ค.

 

์ƒˆ๋กญ๊ฒŒ ์žก์€ ํฌ์ธํŠธ

์ƒ๊ฐํ•ด๋ณผ๋งŒํ•œ ํฌ์ธํŠธ๋Š” TableView Cell์•ˆ์— Collection View๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์ค‘์ฒฉ ๊ตฌ์กฐ๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, ๊ธฐ์กด์— ๋‚˜๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ž‘์—…์„ ์…€ ์•ˆ์—์„œ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋Œ๊ณ  ์™€์„œ ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋” ๋‚˜์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‹œ๊ฐ์ด์—ˆ๋‹ค.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell", for: indexPath) as? MainTableViewCell else {
        return UITableViewCell()
    }

    cell.titleLabel.text = TMDBAPIManager.shared.tvList[indexPath.section].0
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    cell.collectionView.tag = indexPath.section
    cell.collectionView.register(UINib(nibName: "CardCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CardCollectionViewCell")
    cell.collectionView.reloadData() // Index Out of Range ํ•ด๊ฒฐ
    return cell
}

๊ฒฐ๊ตญ์— ๋ฐ์ดํ„ฐ๋ผ๋Š” ๊ฒƒ๋“ค์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๊ด€๋ฆฌ๊ฐ€ ๋˜๊ณ  ์•ก์…˜ ์ฒ˜๋ฆฌ๋„ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์…€ ์•ˆ์—์„œ ์–ด๋–ค ์ฒ˜๋ฆฌ๋“ค์„ ํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๋„ ๊ทธ๋ ‡๊ณ  ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋„ ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

Controller - TableViewCell - CollectionView - CollectionViewCell

 

์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ์ด ํ๋ฆ„์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์†ํ•ด์„œ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กœ์› ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ์ƒˆ๋กœ์šด ๊ด€์ ์„ ์•Œ๊ฒŒ ๋˜์–ด์„œ ์žฌ๋ฐŒ์—ˆ๋‹ค.