본문 바로가기

iOS

UIKit 화면 전환 방법과 예제

728x90
반응형
SMALL

View Controller 의 View 위에 다른 View를 가져와 바꿔치기 예시코드

import UIKit

class ViewController: UIViewController {
    
    var currentView: UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 첫 번째 View 생성 및 추가
        let view1 = UIView(frame: self.view.bounds)
        view1.backgroundColor = .red
        self.view.addSubview(view1)
        self.currentView = view1
        
        // Timer를 사용하여 2초마다 View 변경
        Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { timer in
            if let currentView = self.currentView {
                if currentView.backgroundColor == .red {
                    // 두 번째 View 생성 및 추가
                    let view2 = UIView(frame: self.view.bounds)
                    view2.backgroundColor = .blue
                    self.view.addSubview(view2)
                    currentView.removeFromSuperview()
                    self.currentView = view2
                } else {
                    // 첫 번째 View로 변경
                    currentView.removeFromSuperview()
                    self.view.addSubview(view1)
                    self.currentView = view1
                }
            }
        }
    }
}

 

View Controller 에서 다른 View Controlelr 호출한 화면 전환 예제코드

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Add a button to present the second view controller
        let button = UIButton(type: .system)
        button.setTitle("Present Second View Controller", for: .normal)
        button.addTarget(self, action: #selector(presentSecondViewController), for: .touchUpInside)
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func presentSecondViewController() {
        let secondViewController = SecondViewController()
        present(secondViewController, animated: true, completion: nil)
    }
}

class SecondViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .green
        
        // Add a button to dismiss the second view controller
        let button = UIButton(type: .system)
        button.setTitle("Dismiss", for: .normal)
        button.addTarget(self, action: #selector(dismissViewController), for: .touchUpInside)
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func dismissViewController() {
        dismiss(animated: true, completion: nil)
    }
}

 

Navication Contorller 를 사용한 화면 전환 예제코드

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create a button
        let button = UIButton(type: .system)
        button.setTitle("Go to Second View", for: .normal)
        button.addTarget(self, action: #selector(goToSecondView), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        // Add the button to the view
        view.addSubview(button)
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc private func goToSecondView() {
        let secondViewController = SecondViewController()
        navigationController?.pushViewController(secondViewController, animated: true)
    }
}

class SecondViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .blue
        
        // Create a button
        let button = UIButton(type: .system)
        button.setTitle("Go Back", for: .normal)
        button.addTarget(self, action: #selector(goBack), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        // Add the button to the view
        view.addSubview(button)
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc private func goBack() {
        navigationController?.popViewController(animated: true)
    }
}

화면 전환용 객체 세그웨이(Segueway) 사용한 화면 전환 예제 코드

import UIKit

class ViewController: UIViewController {
    
    let button: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Go to Second View", for: .normal)
        button.addTarget(self, action: #selector(goToSecondVCButtonTapped), for: .touchUpInside)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(button)
        
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func goToSecondVCButtonTapped() {
        let secondVC = SecondViewController()
        secondVC.text = "Hello from First VC"
        navigationController?.pushViewController(secondVC, animated: true)
    }
}

class SecondViewController: UIViewController {
    
    let label: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    var text: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        view.addSubview(label)
        
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
        
        if let text = text {
            label.text = text
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        if isBeingDismissed {
            print("Going back from Second VC")
        }
    }
}
728x90
반응형
LIST

'iOS' 카테고리의 다른 글

SwiftUI active, inavtive, background 상태  (0) 2023.05.18
SwiftUI 이미지 캐시처리  (0) 2023.05.11
WWDC22 Challenge: Learn Switch Control through gaming  (0) 2023.05.07
iOS 연속 탭 위치 인식 방법  (0) 2023.05.06
일급 컬렉션  (0) 2023.05.05