作者:mobiledu2502856483 | 来源:互联网 | 2023-06-08 11:32
我已将标签栏隐藏在我的标签栏控制器中,并改为使用集合视图作为程序的主菜单。为什么?我花了2个小时使它看起来像所需的;标签栏很难根据我的需要进行自定义,我花了数小时没有结果。其余的只是一个标准的控制器。集合视图的委托为选项卡栏控制器设置选定的索引;它工作正常;我唯一的问题来自隐藏标签栏:子视图控制器正在从下方扩展到集合视图的顶部;转到屏幕底部。有没有一种方法可以为VC设置容器,以使子对象的底部位于标签栏可见的位置?我可以这样做但不使用标签栏/将容器的底部与屏幕底部保持一定距离吗?下面的代码,谢谢。
class ProgramMainmenu: UITabBarController {
//MARK: - Properties.
fileprivate lazy var programMainmenu: MenuTypeCV = {
let mainmenu = MenuTypeCV(frame: .zero,collectionViewLayout: UICollectionViewFlowLayout())
mainmenu.defaultSelectedMenuTab = IndexPath(item: 0,section: 0)
mainmenu.delegate = self
mainmenu.dataSource = self
mainmenu.backgroundColor = UIColor.customColoursForAllElements(colourName: "background blue")
return mainmenu
}()
//MARK: - Init.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.customColoursForAllElements(colourName: "background blue")
programMainmenu.register(MainmenuTab.self,forCellWithReuseIdentifier: cellID)
navigationController?.navigationBar.isHidden = true
self.tabBar.isHidden = true
loadchildviewcontrollers()
setMainmenuView()
}
//MARK: - Functions.
fileprivate func setMainmenuView() {
view.addSubview(programMainmenu)
let mainmenuCOnstraints= [
programMainmenu.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -2),programMainmenu.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 8),programMainmenu.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: -8),programMainmenu.heightAnchor.constraint(equalToConstant: 93)
]
NSLayoutConstraint.activate(mainmenuConstraints)
}
fileprivate func loadchildviewcontrollers() {
let test1 = FirstTestController()
let nav1 = UINavigationController(rootViewController: test1)
nav1.navigationBar.isHidden = true
let test2 = SecondTestController()
let nav2 = UINavigationController(rootViewController: test2)
nav2.navigationBar.isHidden = true
let test3 = ThirdTestController()
let nav3 = UINavigationController(rootViewController: test3)
nav3.navigationBar.isHidden = true
let test4 = FourthTestController()
let nav4 = UINavigationController(rootViewController: test4)
nav4.navigationBar.isHidden = true
let test5 = FifthTestController()
let nav5 = UINavigationController(rootViewController: test5)
nav5.navigationBar.isHidden = true
viewCOntrollers= [nav1,nav2,nav3,nav4,nav5]
}
}
extension ProgramMainmenu: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return mainmenuTabs.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = programMainmenu.dequeueReusableCell(withReuseIdentifier: cellID,for: indexPath) as! MainmenuTab
let tab = mainmenuTabs[indexPath.item]
cell.mainmenuTab = tab
return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: programMainmenu.frame.width / 5,height: programMainmenu.frame.height)
}
func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
switch indexPath.item {
case 0: self.selectedIndex = 0
case 1: self.selectedIndex = 1
case 2: self.selectedIndex = 2
case 3: self.selectedIndex = 3
case 4: self.selectedIndex = 4
default: print("no such main tab")
}
}
}