英文:
Xcode 11.3, UInavigationController and UITabbarController facing issues
问题
Brief: 我在UINavigationController
中嵌入了4个控制器,然后通过代码将它们用作UITabbarController
中的选项卡(没有故事板和.xib文件)。
之前我使用的是Xcode 10.3,没有问题,但在切换到11.3后出现了以下问题。
Issues(问题):
-
当我点击选项卡时,控制器的
viewWillAppear
和viewDidAppear
方法没有被调用。 -
导航控制器也有类似的问题,详情请查看 https://stackoverflow.com/q/58254034/5247034。
-
从任何控制器导航到它后,返回(按返回按钮)没有动画效果。
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:
-
when I tap on the tabs, viewWillAppear, and viewDidAppear on the controllers are not getting called.
-
similar issue with navigation controller https://stackoverflow.com/q/58254034/5247034
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论