seminar-2021 icon indicating copy to clipboard operation
seminar-2021 copied to clipboard

NavigationController에 ViewController를 여러개 추가시 background변화

Open eomiso opened this issue 3 years ago • 1 comments

Simulator Screen Recording - iPhone 12 Pro Max - 2021-10-10 at 21 25 18

요약

NavigationController에 .pushViewController 함수로 VC를 추가할 때 왜 첫VC에는 background color가 default로 white이고 두번째 VC 는 color가 없는 것인가요?

첫번째 VC 는 navigation controller가 뒤에 있어서 navigation Controller의 background가 white인게 그대로 투과되어서 보이는 것이고, 두번 째 VC는 그 밑에 첫번째 VC가 깔려있어서 background가 없는게 되는 것인가요?

코드

import UIKit

class MainViewController: UIViewController {
    
    private let button = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Waffle"
        //view.backgroundColor = .white
        
        setButton()
        
        print(navigationController ?? "No navigationController")
    }
    
    func setButton() {
        view.addSubview(button)
        button.setTitle("Press for adding Congroller", for: .normal)
        button.setTitleColor(.systemOrange, for: .normal)
        button.frame = CGRect(x: 100, y: 200, width: 300, height: 52)
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
    }
    
    @objc private func didTapButton() {
        let vc = MainViewController()
        print("adding vc to nc")
        navigationController?.pushViewController(vc, animated: true)
    }
}





eomiso avatar Oct 11 '21 02:10 eomiso

저도 이건 왜 발생하는지 정확히 모르겠네요 ㅎㅎ... 아마 view hierarchy를 확인해보면 나올 것 같기는 한데.... 일단 전에 진섭이형이 설명했듯이 navigation Controller는 자체의 뷰는 의미가 없고 그 아이가 들고있는 뷰가 실질적인 디자인을 맡아야합니다. 그래서 애시당초 저렇게 아무것도 세팅 안하고 그냥 push 하면 뷰컨트롤러가 뷰를 컨트롤안하고 있는거니 어폐가 있는게 맞아요

그래서 navigationController는 navigationBar 디자인 제외하고는 보통 그냥 background color를 clear로 둡니다

Ethan-MoBeau avatar Nov 02 '21 14:11 Ethan-MoBeau