코드로만 화면 전환을 하기 위해서는 Scene Delegate 파일에서 네비게이션 컨트롤러를 뷰 컨트롤러 밑에 깔아야 한다.
Scene Delegate 파일에서 scene(scene:session:connectionOptions:) 함수에 아래의 코드를 추가한다.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
// 초기 뷰 컨트롤러 생성
let initialViewController = ViewController()
// 네비게이션 컨트롤러 생성
let navigationController = UINavigationController(rootViewController: initialViewController)
// 네비게이션 컨트롤러를 윈도우의 루트 뷰 컨트롤러로 설정
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}

import UIKit
class SecondViewController: UIViewController {
private lazy var myButton: UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
// button.layer.borderColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
button.setTitle("ToFirstViewController", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
// button.isEnabled = false
button.addTarget(self, action: #selector(myButtonTapped), for: .touchUpInside)
view.addSubview(button)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myButtonSetup()
view.backgroundColor = .white
}
@objc func myButtonTapped() {
self.navigationController?.popViewController(animated: true)
}
func myButtonSetup() {
myButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
myButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
myButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
myButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
myButton.heightAnchor.constraint(equalToConstant: 40)
])
}
}
화면 전환을 위한 SecondViewController 를 생성하고 되돌아가는 버튼을 하나 만든다.

import UIKit
class ViewController: UIViewController {
// 화면 전환을 위한 버튼
private lazy var myButton: UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
// button.layer.borderColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
button.setTitle("ToSecondViewController", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
// button.isEnabled = false
button.addTarget(self, action: #selector(myButtonTapped), for: .touchUpInside)
view.addSubview(button)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
myButtonSetup()
view.backgroundColor = .white
}
// 버튼이 클릭됐을 때 (화면 전환)
@objc func myButtonTapped() {
print("myButtonTapped() 시작")
let secondVC = SecondViewController()
//secondViewController.data = "데이터전달"
// 내비게이션 컨트롤러를 통해 화면 전환
self.navigationController?.pushViewController(secondVC, animated: true)
print("myButtonTapped() 완료")
}
// 버튼 오토레이아웃
func myButtonSetup() {
myButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
myButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
myButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
myButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
myButton.heightAnchor.constraint(equalToConstant: 40)
])
}
}
그 후 일반적인 코드중심의 네비게이션 화면 전환처럼 똑같이 전환하면 된다.
[Navigation] Storyboard + Code2 (pushViewController()로 화면 전환)
Navigation 을 코드로 객체생성하여 push()로 화면 전환 [Navigation] Storyboard + Code (pushViewController()로 화면 전환)Navigation 을 스토리보드로 객체생성하여 push()로 화면 전환 // 객체 생성guard let secondViewC
usiacode.tistory.com
[버튼스닛펫-코드로만 버튼]
[UIKit CodeSnippets] UIButton Snippet (Only-code)
+ Snippet 추가 설명 Xcode 코드 스닛펫, Code snippet코드 스닛펫(Code snippet)이란 코드의 단편, 조각이란 뜻으로 자동완성기능을 의미한다.애플에서 미리 만들어둔 템플릿도 많으며, 커스텀을 할 수 있
usiacode.tistory.com
[오토레이아웃]
AutoLayout - Code
코드를 통한 오토레이아웃 설정 오토레이아웃(AutoLayout)이란 화면의 위치와 크기를 동적으로 정의하고 관리하는 방식제약조건(Constraints)을 통해 설정하여 뷰 안에서의 관계를 정의할 수 있다.뷰
usiacode.tistory.com
'UIKit' 카테고리의 다른 글
| [Navigation Controller] Code로 세팅 (0) | 2024.08.01 |
|---|---|
| [Navigation Controller] Storyboard로 세팅 (0) | 2024.08.01 |
| [Navigation] Storyboard + Code2 (pushViewController()로 화면 전환) (0) | 2024.08.01 |
| [Navigation] Storyboard + Code1 (pushViewController()로 화면 전환) (0) | 2024.07.31 |
| [Navigation] Storyboard + Code (Manual Segue-performSegue()으로 화면 전환) (0) | 2024.07.31 |