ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 탭바와 네비게이션바를 모두 쓰고 싶었던 침팬치 이야기
    프로젝트/삽질 2023. 2. 16. 01:35

    바보 이야기

    탭바와 네비게이션 바를 다 배우고 같이는 못 적용시키는 어느 바보의 이야기다.

    우선, 첫 화면부터 보여주고 싶었기 때문에 SceneDelegate의 willConnectTo에 구현했다.

    -> 구글링을 하면 AppDelegate의 didFinishLaunchingWithOptions에 구현하라고 하는 글들이 많다. 구현사항은 크게 차이가 나지 않으나, AppDelegate로 구현하려면 SceneDelegate를 없애야하기 때문에 아이패드용과 아이폰 용을 분리해서 구현해야 하나 구분이 가지 않아, 잘 알고 있는 SceneDelegate의 구현하기로 했다..

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: windowScene)
    
            // MARK: - UITabBar구현
            let tabVC = UITabBarController()
            let tab1 = BlogPostViewController()
            let tab2 = BookmarkViewController()
            let tab3 = ConfigurationViewController()
            let tab4 = AppInfoViewController()
    
            tab1.title = "목록"
            tab2.title = "즐겨찾기"
            tab3.title = "설정"
            tab4.title = "앱 정보"
            tabVC.setViewControllers([tab1,tab2,tab3,tab4], animated: false)
            tabVC.modalPresentationStyle = .fullScreen
            tabVC.tabBar.backgroundColor = .white
    
            guard let items = tabVC.tabBar.items else {return}
    
            items[0].image = UIImage(systemName: "list.star")
            items[1].image = UIImage(systemName: "list.bullet.circle")
            items[2].image = UIImage(systemName: "gearshape.fill")
            items[3].image = UIImage(systemName: "ellipsis")
            window?.rootViewController = tabVC
            window?.makeKeyAndVisible()
        }
    

    호기롭게 탭바 설정을 마친 뒤, 네비게이션 바를 설정했다.

    // SceneDelegate 내부
    let vc1 = UINavigationController(rootViewController: BlogPostViewController())
    vc1.present(tabBarVC, animated: true, completion: nil)
    
    //탭바로 보여줄 컨트롤러 내부
        func setNavi() { // 네비게이션 바 appearance 객체 설정
    
            let navigationBarAppearance = UINavigationBarAppearance()
                    navigationBarAppearance.configureWithOpaqueBackground()
                    navigationController?.navigationBar.standardAppearance = navigationBarAppearance
                    navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
                    navigationController?.navigationBar.tintColor = .blue
    
                    navigationItem.scrollEdgeAppearance = navigationBarAppearance
                    navigationItem.standardAppearance = navigationBarAppearance
                    navigationItem.compactAppearance = navigationBarAppearance
    
                    navigationController?.setNeedsStatusBarAppearanceUpdate()
    
                    navigationController?.navigationBar.isTranslucent = false
                    navigationController?.navigationBar.backgroundColor = .white
                    title = "목록"
        }

    당연히 실행될리가 없었다.

    • presenting view controller is discouraged 이런 요상한 에러가 발생

    해결

    • 탭바의 탭 하나가 객체라는 걸 잊고 있었다.. 내가 시도했던 코드는 별도의 네비게이션 컨트롤러 객체 + 탭바의 첫 탭으로 같은 BlogPostViewController()를 지정했기 때문에, 저런 에러가 발생했던 것이다.
    • 실제로 실행이 안되지는 않았다. 워낙 빠르게 실행되서 처음에 못봤지만 실행 과정은
      • 네비게이션 컨트롤러가 포함된 BlogPostViewController()가 먼저 화면에 present
      • 다음 코드로 탭뷰의 탭1(이것도 BlogPostViewController())을 표시해야 하기 때문에, 네비게이션 컨트롤러 포함 객체는 빠르게 사라짐.
      • window?.makeKeyAndVisible()에 의해 실제 화면에 표시
     let tab1 = UINavigationController(rootViewController: BlogPostViewController())
    • 해결책은 너무 쉽다. 탭바 배열의 탭 1개는 하나의 화면이라고 보면 되기에 그걸 네비게이션 컨트롤러로 바꿔주면 된다! 물론 탭바의 탭마다 네비게이션 바를 추가하고 싶다면 전부 네비게이션 컨트롤러로 선언하면 되겠죠?

    교훈 : 클론코딩이나 구글링을 통해 코드를 알았다면 적용하기 전에 어떤 건지 이해는 좀 하고 쓰자😅

    '프로젝트 > 삽질' 카테고리의 다른 글

    다크모드를 구현해보자.  (0) 2023.02.20

    댓글

Designed by Tistory.