ios - 试图找到自定义标签栏的更好方法
问题描述
我正在尝试创建一个自定义TabBar
. 到目前为止,我的方法是创建一个UIViewController
(我们称之为TabBarController
),在 中TabBarController
,我添加了一个childVC
(我们称之为UserViewController
)。
我想不出一种方法来改变UserViewController
不让TabBarController
消失。
我开始认为这可能不是一个好的选择,而且大多数情况下都有更好的、实际可行的方法来做到这一点。我真正想要实现的只是一个TabBarController
,与 分开UserViewController
,TabBarController
将始终显示在屏幕底部,通过单击其中的项目,UserViewController
将相应地更改。
我搜索了几个小时,尝试了不同的解决方案,但没有任何效果。真的希望您能指导我,也许分享您对此的教程或文章。
解决方案
创建 的子类UITabBarViewController
。
class TabBarController: UITabBarController {
var previousTabIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
let viewControllerOne = ViewController()
viewControllerOne.tabBarItem.image = UIImage(named: "one")
viewControllerOne.tabBarItem.title = "One"
let viewControllerTwo = ViewController()
viewControllerTwo.tabBarItem.image = UIImage(named: "two")
viewControllerTwo.tabBarItem.title = "Two"
let viewControllerThree = ViewController()
viewControllerThree.tabBarItem.title = "Premium"
viewControllerThree.tabBarItem.image = UIImage(named: "three")
let tabBarList = [
viewControllerOne,
viewControllerTwo,
viewControllerThree
]
self.viewControllers = tabBarList.map {
let nav = UINavigationController(rootViewController: $0)
return nav
}
}
}
然后在SceneDelegate
, 设置 window.rootViewController = TabBarController()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = TabBarController()
self.window = window
window.makeKeyAndVisible()
}
}
如果您在旧版本的 xCode 中创建了项目,请分配TabBarController()
给window.rootController
in didFinishLaunchingWithOptions
ofAppDelegate
推荐阅读
- c# - C# selenium instagram 登录问题
- python - 无法在 jupyter 笔记本中将字符串转换为浮点数
- python - 同时对多个文档使用 TF-IDF Vectorizer
- python - 搜索多个关键字python
- java - PowerMockito:无法模拟 Calendar.getInstance
- java - E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping 对 device_type=5 失败,ret=-1 Rxjava+Retrofit
- css - JS/CSS 动画在第一个循环后触发两次
- python - 第 66 行,在
c=menu() File "System-3.py", line 13, in menu StudentDetails() NameError: global name 'StudentDetails' is not defined - .net - 如何检查 DB-First MVC 中的最大长度?
- python - 如何使用 word2vec 获取情感标签