iT邦幫忙

2022 iThome 鐵人賽

DAY 30
0
Mobile Development

IOS新手之旅系列 第 30

IOS新手之旅 Day30:各種奇怪問題(2)

  • 分享至 

  • xImage
  •  

昨天分享了遇到的奇怪問題,今天來分享另外一個。

Tabbar和NavigationBar

當新增完Tabbar之後,執行起來發現,Tabbar和Navigationbar的設定都跑掉了,原因是因為這裡將Tabbar作為子物件加到Naviationbar下面,正確來說應該是在上面才對。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowSence = (scene as? UIWindowScene) else { return }
        
        let rootVC1 = AlarmViewController(nibName: String(describing: AlarmViewController.self), bundle: nil)
        let navigationVC1 = UINavigationController(rootViewController: rootVC1)
        
        let MyTabBar = UITabBarController()
        
        let AlarmVC = AlarmViewController()
        AlarmVC.tabBarItem.image = UIImage(systemName: "clock")
        AlarmVC.tabBarItem.title = "鬧鐘"
        
        let StopWatchVC = StopWatchViewController()
        StopWatchVC.tabBarItem.image = UIImage(systemName: "stopwatch")
        StopWatchVC.tabBarItem.title = "碼表"
        
        let WorldClockVC = WorldClockViewController()
        WorldClockVC.tabBarItem.image = UIImage(systemName: "globe")
        WorldClockVC.tabBarItem.title = "世界時鐘"
        
        let TimerVC = TimerViewController()
        TimerVC.tabBarItem.image = UIImage(systemName: "timer")
        TimerVC.tabBarItem.title = "計時器"
        
        MyTabBar.viewControllers = [AlarmVC, StopWatchVC, WorldClockVC, TimerVC]
        MyTabBar.selectedIndex = 0
        
        window = UIWindow(frame: windowSence.coordinateSpace.bounds)
        window?.windowScene = windowSence
        window?.rootViewController = navigationVC1
        window?.makeKeyAndVisible()
        
        AlarmVC.addChild(MyTabBar)
    }

正確的程式碼應該要像下面這樣。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowSence = (scene as? UIWindowScene) else { return }
        
        let rootVC1 = AlarmViewController(nibName: String(describing: AlarmViewController.self), bundle: nil)
        let navigationVC1 = UINavigationController(rootViewController: rootVC1)
        
        let rootVC2 = StopWatchViewController(nibName: String(describing: StopWatchViewController.self), bundle: nil)
        let navigationVC2 = UINavigationController(rootViewController: rootVC2)
        
        let rootVC3 = WorldClockViewController(nibName: String(describing: WorldClockViewController.self), bundle: nil)
        let navigationVC3 = UINavigationController(rootViewController: rootVC3)
        
        let rootVC4 = TimerViewController(nibName: String(describing: TimerViewController.self), bundle: nil)
        let navigationVC4 = UINavigationController(rootViewController: rootVC4)
        
        let MyTabBar = UITabBarController()
        
        let AlarmVC = navigationVC1
        AlarmVC.tabBarItem.image = UIImage(systemName: "clock")
        AlarmVC.tabBarItem.title = "鬧鐘"
        
        let StopWatchVC = navigationVC2
        StopWatchVC.tabBarItem.image = UIImage(systemName: "stopwatch")
        StopWatchVC.tabBarItem.title = "碼表"
        
        let WorldClockVC = navigationVC3
        WorldClockVC.tabBarItem.image = UIImage(systemName: "globe")
        WorldClockVC.tabBarItem.title = "世界時鐘"
        
        let TimerVC = navigationVC4
        TimerVC.tabBarItem.image = UIImage(systemName: "timer")
        TimerVC.tabBarItem.title = "計時器"
        
        MyTabBar.viewControllers = [AlarmVC, StopWatchVC, WorldClockVC, TimerVC]
        MyTabBar.selectedIndex = 0
        
        window = UIWindow(frame: windowSence.coordinateSpace.bounds)
        window?.windowScene = windowSence
        window?.rootViewController = MyTabBar
        window?.makeKeyAndVisible()
    }

這樣就不會有問題了。

那麼30天的鐵人賽就到這裡結束啦。


上一篇
IOS新手之旅 Day29:各種奇怪問題(1)
系列文
IOS新手之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言