⎮ UINavigation을 활용한 View 전환
UINavigation을 학습하게 되면서, UINavigation을 활용한 View 전환을 하기 위해 View를 2개 만들었다.
// firstView.swift
class ViewController: UIViewController {
private let label: UILabel = {
let label = UILabel()
label.text = "친구 목록"
label.textColor = .black
label.font = .boldSystemFont(ofSize: 30)
label.textAlignment = .center
return label
}()
private lazy var addButton: UIButton = {
let button = UIButton()
button.setTitle("추가", for: .normal)
button.setTitleColor(.gray, for: .normal)
button.backgroundColor = .white
button.addTarget(self, action: #selector(buttonTapped), for: .touchDown)
return button
}()
private let tableView: UITableView = {
let tableView = UITableView()
tableView.register(TableViewCell.self, forCellReuseIdentifier: TableViewCell.id)
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = true
}
func configureUI() {
view.backgroundColor = .white
[label, addButton, tableView].forEach { view.addSubview($0) }
label.snp.makeConstraints {
$0.top.equalToSuperview().offset(70)
$0.centerX.equalToSuperview()
}
addButton.snp.makeConstraints {
$0.centerY.equalTo(label.snp.centerY)
$0.trailing.equalToSuperview().offset(-30)
}
tableView.snp.makeConstraints {
$0.top.equalTo(label.snp.bottom).offset(70)
$0.bottom.equalToSuperview().offset(-60)
$0.leading.equalToSuperview().offset(20)
$0.trailing.equalToSuperview().offset(-20)
$0.centerX.equalTo(label.snp.centerX)
}
}
@objc func buttonTapped() {
let secondView = PhoneBookViewController()
navigationController?.pushViewController(secondView, animated: false)
dismiss(animated: false)
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
70
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.id) as? TableViewCell else {
return UITableViewCell()
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
7
}
}
//secondeView.swift
class PhoneBookViewController: ViewController {
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "")
imageView.layer.cornerRadius = 70
imageView.layer.borderWidth = 2
imageView.layer.borderColor = UIColor.gray.cgColor
return imageView
}()
let createButton: UIButton = {
let button = UIButton()
button.setTitle("랜덤 이미지 생성", for: .normal)
button.setTitleColor(.gray, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 15)
return button
}()
let nameTextField: UITextField = {
let textfield = UITextField()
textfield.borderStyle = .roundedRect
textfield.font = .systemFont(ofSize: 15)
return textfield
}()
let phoneNumberTextField: UITextField = {
let textfield = UITextField()
textfield.borderStyle = .roundedRect
textfield.font = .systemFont(ofSize: 15)
return textfield
}()
override func viewDidLoad() {
super.viewDidLoad()
secondConfigureUI
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = false
}
func secondConfigureUI() {
view.backgroundColor = .white
[profileImageView, createButton, nameTextField, phoneNumberTextField].forEach { view.addSubview($0) }
profileImageView.snp.makeConstraints {
$0.width.height.equalTo(140)
$0.top.equalToSuperview().offset(120)
$0.centerX.equalToSuperview()
}
createButton.snp.makeConstraints {
$0.top.equalTo(profileImageView.snp.bottom).offset(20)
$0.centerX.equalToSuperview()
}
nameTextField.snp.makeConstraints {
$0.top.equalTo(createButton.snp.bottom).offset(30)
$0.centerX.equalToSuperview()
$0.leading.trailing.equalToSuperview().inset(30)
$0.height.equalTo(30)
}
phoneNumberTextField.snp.makeConstraints {
$0.top.equalTo(nameTextField.snp.bottom).offset(30)
$0.centerX.equalToSuperview()
$0.leading.trailing.equalToSuperview().inset(30)
$0.height.equalTo(30)
}
}
}
이렇게 하고 빌드를 했는데 다음과 같은 오류를 마주했다.
컴퓨터한테 가끔 삐질 뻔한다.
⎮ view가 겹쳐버린 이유
아주 허술한 이유인데, view가 겹쳐버린 이유는 다음과 같다.
오마이갓.
오류 개발자의 명성답게 UIViewController가 아니라 viewController를 상속해버린 것이다.
어쩐지, configureUI를 쓰면, overriding하라고 나오더니......
class PhoneBookViewController: UIViewController {
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "")
imageView.layer.cornerRadius = 70
imageView.layer.borderWidth = 2
imageView.layer.borderColor = UIColor.gray.cgColor
return imageView
}()
let createButton: UIButton = {
let button = UIButton()
button.setTitle("랜덤 이미지 생성", for: .normal)
button.setTitleColor(.gray, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 15)
return button
}()
let nameTextField: UITextField = {
let textfield = UITextField()
textfield.borderStyle = .roundedRect
textfield.font = .systemFont(ofSize: 15)
return textfield
}()
let phoneNumberTextField: UITextField = {
let textfield = UITextField()
textfield.borderStyle = .roundedRect
textfield.font = .systemFont(ofSize: 15)
return textfield
}()
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = false
}
private func configureUI() {
view.backgroundColor = .white
[profileImageView, createButton, nameTextField, phoneNumberTextField].forEach { view.addSubview($0) }
profileImageView.snp.makeConstraints {
$0.width.height.equalTo(140)
$0.top.equalToSuperview().offset(120)
$0.centerX.equalToSuperview()
}
createButton.snp.makeConstraints {
$0.top.equalTo(profileImageView.snp.bottom).offset(20)
$0.centerX.equalToSuperview()
}
nameTextField.snp.makeConstraints {
$0.top.equalTo(createButton.snp.bottom).offset(30)
$0.centerX.equalToSuperview()
$0.leading.trailing.equalToSuperview().inset(30)
$0.height.equalTo(30)
}
phoneNumberTextField.snp.makeConstraints {
$0.top.equalTo(nameTextField.snp.bottom).offset(30)
$0.centerX.equalToSuperview()
$0.leading.trailing.equalToSuperview().inset(30)
$0.height.equalTo(30)
}
}
}
위의 코드와 같이 제대로 UIViewController를 상속해주고나서
아주 쉽게 문제를 해결할 수 있었다.
컴퓨터야 우리 다시 사이좋게 지내자!
'Swift > 오류 개발자' 카테고리의 다른 글
Swift | Custom Font 적용이 안되는 오류 해결하기 (0) | 2025.04.07 |
---|---|
Swift | Converting non-sendable function value to '@Sendable () -> ()' may introduce data races 오류 이해하기 (0) | 2025.04.05 |
Swift | presentingViewController.dismiss 오류(feat. 휴먼 에러) (0) | 2025.03.26 |
Swift | SnapKit no such file or directory 오류 (2) | 2025.03.18 |
Swift | TableView에서 특정 영역만 선택이 가능하도록 만들기 (0) | 2025.03.09 |