我有一个带有三个选项的标签栏,当我尝试转到配置文件页面时,底部标签栏被我以编程方式添加的集合视图覆盖。如何确保标签栏是这个系列没有涵盖?
这是ProfilePageVC:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
let flowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.cyan
// self.lowerView.addSubview(collectionView)
self.lowerView.insertSubview(collectionView, belowSubview: self.view)
self.reloadInputViews()
}
这是我的标签条形码所在的位置(它只是普通的默认VC)
class ViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
var homeVC : UIViewController!
var testVC : UIViewController!
var profileVC : UIViewController!
var viewControllers : [UIViewController]!
//What tab we are pressing
var selectedVC : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
homeVC = storyboard.instantiateViewController(withIdentifier: "HomePage")
testVC = storyboard.instantiateViewController(withIdentifier: "RecordPage")
profileVC = storyboard.instantiateViewController(withIdentifier: "ProfilePage")
viewControllers = [homeVC, testVC, profileVC]
buttons[selectedVC].isSelected = true
hasPressedTab(buttons[selectedVC])
}
@IBOutlet var buttons: [UIButton]!
@IBAction func hasPressedTab(_ sender: UIButton) {
let previousVC = selectedVC
selectedVC = sender.tag
buttons[previousVC].isSelected = false
let previousIndex = viewControllers[previousVC]
//removes from previous VC
previousIndex.willMove(toParentViewController: nil)
previousIndex.view.removeFromSuperview()
previousIndex.removeFromParentViewController()
//accesses current button selected
sender.isSelected = true
let vc = viewControllers[selectedVC]
addChildViewController(vc)
//addjusts size of VC to match the added contentView
vc.view.frame = contentView.bounds
contentView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
}
以下是描述问题的图片:
棕褐色区域是lowerView
With-Out Collection View作为子视图: https://ibb.co/gH2dWF
将CollectionView作为子视图: https://ibb.co/jWh1ka
答案 0 :(得分:0)
我不确定“lowerView”是指什么,但您是否尝试过将子视图添加到视图中?
self.view.addSubview(collectionView)
答案 1 :(得分:0)
我认为问题可能在于这一行:
self.lowerView.insertSubview(collectionView, belowSubview: self.view)
为什么要尝试在lowerView中添加它?我实际上无法看到lowerView在代码中的实际含义,但正常的流程是将collectionView添加为self.view
的子视图。也许lowerView框架实际上在tabBar上,这就是它覆盖标签栏的原因。
代码如下:
self.view.addSubview(collectionView)
如果您需要将集合视图发送到后面,可以使用以下命令发送:
self.view.sendSubview(toBack: collectionView)