Search
💁🏻‍♂️

4. [No StoryBoard] UI Component 구성하기

클로져를 사용해서 프로퍼티 초기화

클로져를 사용해서 프로퍼티 초기화

private let logoImageView: UIImageView = { let iv = UIImageView() iv.contentMode = .scaleAspectFit iv.clipsToBounds = true iv.image = UIImage(named: "TwitterLogo") return iv }()
Swift
복사

헬퍼함수에서 레이아웃 잡아주기

func configureUI() { view.addSubview(logoImageView) logoImageView.snp.makeConstraints { make in make.centerX.equalToSuperview() make.top.equalToSuperview().offset(50) make.size.equalTo(150) } }
Swift
복사

반복되는 코드는 함수로 따로 정의해서 코드 길이 축소

class Utillities { func inputContainerView(withImage image: UIImage, textFeild: UITextField) -> UIView { let view = UIView() view.snp.makeConstraints { make in make.height.equalTo(50) } let iv = UIImageView() iv.image = image view.addSubview(iv) iv.snp.makeConstraints { make in make.centerY.leading.equalToSuperview() make.size.equalTo(24) } view.addSubview(textFeild) textFeild.snp.makeConstraints { make in make.leading.equalTo(iv.snp.trailing).offset(8) make.top.equalToSuperview().offset(8) make.bottom.equalToSuperview().offset(-8) make.trailing.equalToSuperview().offset(-20) } let seperator = UIView() seperator.backgroundColor = .white view.addSubview(seperator) seperator.snp.makeConstraints { make in make.leading.equalTo(iv) make.bottom.trailing.equalToSuperview() make.height.equalTo(0.75) } return view }
Swift
복사
// MARK: 이하 두개의 뷰가 공통된 컴포넌트를 가지므로 static 한 UI 는 함수레벨로 복붙. // 컨테이너뷰 1 private lazy var emailContainerView: UIView = { let image = UIImage(named: "ic_mail_outline_white_2x-1")! let view = Utillities().inputContainerView(withImage: image,textFeild: emailTextFeild) return view }() // 컨테이너뷰 2 private lazy var passwordContainerView: UIView = { let image = UIImage(named: "ic_lock_outline_white_2x")! let view = Utillities().inputContainerView(withImage: image,textFeild: passwordTextFeild) return view }()
Swift
복사