[Navigation Controller] Storyboard 설정
Navigation 컨트롤러를 추가하여 상단 네비게이션 바 설정 뷰 컨트롤러에서 오른쪽 하단의 Navigation Controller 를 클릭 추가된 뒤, 잘 보면 Navigation Item 이라는 객체가 이미 위에 생겨있다.클릭
usiacode.tistory.com
스토리보드와의 설정과는 다르게, 네비게이션 컨트롤러를 뷰 컨트롤러 이 전에 깔아야 하기 때문에, 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()
}
새로운 뷰 컨트롤러 SecondViewController 생성
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("되돌아가기", 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.
view.backgroundColor = .white
myButtonSetup()
}
// 버튼 클릭시 실행되는 함수 (되돌아가기 - popViewController)
@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: 30),
myButton.heightAnchor.constraint(equalToConstant: 40)
])
}
}
그 후 뷰 컨트롤러에서 아래와 같이 코드를 작성하여 타이틀과 Bar Button Item 을 세팅할 수 있다.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// 네비게이션 바의 아이템에서 타이틀 수정
navigationItem.title = "Code Navi"
// 네비게이션 Bar Button Item 추가
// action 에는 $selector 를 통해 클릭됐을 때 호출되는 함수의 이름을 적는다.
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(navigationBarButtonTapped))
}
@objc func navigationBarButtonTapped() {
let secondVC = SecondViewController()
// secondVC.modalPresentationStyle = .fullScreen
self.present(secondVC, animated: true, completion: nil)
}
}
'UIKit' 카테고리의 다른 글
[TabBar] Code로 구현 (0) | 2024.08.02 |
---|---|
[TabBar] Storyboard로 구현 (0) | 2024.08.02 |
[Navigation Controller] Storyboard로 세팅 (0) | 2024.08.01 |
[Navigation] Only Code (pushViewController()로 화면 전환) (0) | 2024.08.01 |
[Navigation] Storyboard + Code2 (pushViewController()로 화면 전환) (0) | 2024.08.01 |