Xcode 11.3、UINavigationController 和UITabBarController 遇到问题。

huangapple go评论83阅读模式
英文:

Xcode 11.3, UInavigationController and UITabbarController facing issues

问题

Brief: 我在UINavigationController中嵌入了4个控制器,然后通过代码将它们用作UITabbarController中的选项卡(没有故事板和.xib文件)。

之前我使用的是Xcode 10.3,没有问题,但在切换到11.3后出现了以下问题。

Issues(问题):

  1. 当我点击选项卡时,控制器的viewWillAppearviewDidAppear方法没有被调用。

  2. 导航控制器也有类似的问题,详情请查看 https://stackoverflow.com/q/58254034/5247034

  3. 从任何控制器导航到它后,返回(按返回按钮)没有动画效果。

Code TabbarController(代码 TabbarController):

final class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.viewControllers = [
            TabBarItems.home.tabController,
            TabBarItems.search.tabController,
            TabBarItems.cart.tabController,
            TabBarItems.account.tabController
        ]
        
        self.tabBar.isTranslucent = false
    }
}

extension TabBarController {
    enum TabBarItems {
        case home
        case search
        case cart
        case account
        
        var title: String {
            switch self {
            
            case .home:
                return "主页"
            case .search:
                return "搜索"
            case .cart:
                return "购物袋"
            case .account:
                return "账户"
            }
        }
        
        var tabController: UINavigationController {
            switch self {
            
            case .home:
                return createNavController(viewController: HomeController(), title: self.title, imageName: Assets.home.rawValue)
            case .search:
                return createNavController(viewController: UIViewController(), title: self.title, imageName: Assets.search.rawValue)
            case .cart:
                return createNavController(viewController: BagController(), title: self.title, imageName: Assets.shoppingBag.rawValue)
            case .account:
                return createNavController(viewController: AccountController(), title: self.title, imageName: Assets.account.rawValue)
            }
        }
        
        private func createNavController(viewController: UIViewController, title: String, imageName: String) -> UINavigationController {
            let navController = UINavigationController(rootViewController: viewController)
            viewController.view.backgroundColor = .white
            navController.tabBarItem.title = title
            navController.tabBarItem.image = imageName.image
            return navController
        }
    }
}
英文:

Brief: I have 4 controllers embedded in UINavigationController, who are then used as tabs in UITabbarController, (No storyboards and .xib's) through code.

Previously I was using Xcode 10.3, there were no issues but after I changed to 11.3 these issues came up.

Issues:

  1. when I tap on the tabs, viewWillAppear, and viewDidAppear on the controllers are not getting called.

  2. similar issue with navigation controller https://stackoverflow.com/q/58254034/5247034

  3. Pop back (pressing back button) from any controller after navigating to it is not getting animated.

Code TabbarController:

final class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.viewControllers = [
            TabBarItems.home.tabController,
            TabBarItems.search.tabController,
            TabBarItems.cart.tabController,
            TabBarItems.account.tabController
        ]
        
        self.tabBar.isTranslucent = false
    }
}

extension TabBarController {
    enum TabBarItems {
        case home
        case search
        case cart
        case account
        
        var title: String {
            switch self {
            
            case .home:
                return "Home"
            case .search:
                return "Search"
            case .cart:
                return "Bag"
            case .account:
                return "Account"
            }
        }
        
        var tabController: UINavigationController {
            switch self {
            
            case .home:
                return createNavController(viewController: HomeController(), title: self.title, imageName: Assets.home.rawValue)
            case .search:
                return createNavController(viewController: UIViewController(), title: self.title, imageName: Assets.search.rawValue)
            case .cart:
                return createNavController(viewController: BagController(), title: self.title, imageName: Assets.shoppingBag.rawValue)
            case .account:
                return createNavController(viewController: AccountController(), title: self.title, imageName: Assets.account.rawValue)
            }
        }
        
        private func createNavController(viewController: UIViewController, title: String, imageName: String) -> UINavigationController {
            let navController = UINavigationController(rootViewController: viewController)
            viewController.view.backgroundColor = .white
            navController.tabBarItem.title = title
            navController.tabBarItem.image = imageName.image
            return navController
        }
    }
}

答案1

得分: 0

问题出在一个在XCode 10.3中创建的项目上,尝试在XCode 11+中直接运行它。

在iOS 13/XCode 11之前,应用程序的起点是AppDelegate,但现在在Xcode 11+中,SceneDelegate也承担了一部分责任。即window的配置应该在scene(:willConnectTo:)函数中完成。

我忘了添加SceneDelegate.swift文件和相应的Info.plist键。

感谢@DonMag的帮助。

英文:

The issue was with a project created in XCode 10.3 and trying to run it out of the box in XCode 11+.

As prior to iOS 13/ XCode11, the apps starting point was AppDelegate but now in Xcode 11+, SceneDelegate shared some of the responsibility. i.e. the window configuration should be done in scene(:willConnectTo:) function.

I forgot to add SceneDelegate.swift and Info.plist key for that.

Thanks @DonMag for helping me out.

huangapple
  • 本文由 发表于 2020年1月3日 19:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定