sta*_*tan 8 uiviewcontroller uitabbar ios
我的应用程序的结构如下:
UITabBarController -> UINavigationController -> [UIViewController1, UIViewController2, UIViewController3]
我需要实现的是在小框架中的 tabBar 上方显示和隐藏子 UIViewController,因此它在导航堆栈中的所有控制器上都是可见的。因此,当用户在堆栈中来回导航时,如果添加了该子项,则它必须在所有控制器上可见。
我已经尝试将孩子添加到 UITabBarController 并且它工作正常,我遇到的问题是阴影标签栏项目添加到标签栏,这是我不想要的。
我曾尝试将孩子添加到导航控制器,但是在堆栈中导航时会增加其他问题,它会关闭孩子而不是自我并加载相同的控制器。
有没有人有关于如何在整个导航过程中保留这个子控制器的建议。
我在这里搜索了任何建议,但没有一个像我的情况,所以没有帮助。
谢谢
我假设您以编程方式将容器视图添加到标签栏控制器中,然后将子视图控制器添加到该容器视图中。我对吗?
如果是这种情况,tabbar 控制器将子控制器添加到其viewControllers数组中。
您可以viewControllers?.removeLast()在添加孩子后立即致电来解决此问题。
这段代码对我有用:
override func viewDidLoad() {
super.viewDidLoad()
let containerView = UIView()
view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true
containerView.leftAnchor.constraint(equalTo: tabBar.leftAnchor, constant: 40).isActive = true
containerView.rightAnchor.constraint(equalTo: tabBar.rightAnchor, constant: -40).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
if let childVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildViewController") {
addChild(childVC)
containerView.addSubview(childVC.view)
childVC.didMove(toParent: self)
childVC.view.translatesAutoresizingMaskIntoConstraints = false
childVC.view.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
childVC.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
childVC.view.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
childVC.view.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
if let childIndex = viewControllers?.firstIndex(of: childVC) {
viewControllers?.remove(at: childIndex)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,而不是仅仅调用removeLast()我检查了它childVC是否确实在该数组中。只是为了更安全。
| 归档时间: |
|
| 查看次数: |
1196 次 |
| 最近记录: |