Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[iOS] Protocol -> BaseView #6

@seungchan2

Description

@seungchan2
import UIKit

class BaseView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
        setupConstraints()
    }
    
    @available(*, unavailable)
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func setupView() {}
    func setupConstraints() {}
}

BaseView 설계 이유

  • 처음에는 프로토콜을 사용해서 setupView()와 setupConstraints()를 사용했었음
  • 그럴바엔 하나의 BaseView로 관리하는 게 효율적이라서 체계를 바꿈
  • 필요할 때 override해서 사용하면 됨

예시 코드

//
//  AddPillSecondView.swift
//  SobokSobok
//
//  Created by 김승찬 on 2022/04/29.
//
import UIKit

import SnapKit
import Then

final class FirstView: BaseView {
    
    lazy var nameLabel = UILabel().then {
        $0.text = "HI"
    }
    
    override func setupView() {
        addSubview(nameLabel)
    }
    
    override func setupConstraints() {
        nameLabel.snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.centerY.equalToSuperview()
        }
    }
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions