Я создал коллекционное представление, которое действует как панель навигации, и я сделал это базовым контроллером представления (TabBar.swift), и оно будет отображаться на всех других моих контроллерах представления.на данный момент у него есть 3 ячейки, и нажатие на каждую ячейку направит пользователя к соответствующему контроллеру представления.Контроллерами 3 представлений являются FirstScreen.swift, SecondScreen.swift и ThirdScreen.swift.Вот код контроллера базового представления:
TabBar.swift
struct OpenViewController {
var viewController: UIViewController
}
class TabBar: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
// Call all the elements
setupCollectionView()
view.backgroundColor = .white
}
let viewControllerList = [
OpenViewController(viewController: FirstScreen()),
OpenViewController(viewController: SecondScreen()),
OpenViewController(viewController: ThirdScreen()),
]
// Setup the collection veiw
func setupCollectionView() {
collectionView?.backgroundColor = .clear
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
collectionView.showsHorizontalScrollIndicator = false
view.addSubview(collectionView)
addCollectionViewConstraints()
}
// Add constraints to the collection view
func addCollectionViewConstraints() {
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true
}
// The number of elements inside the collection view
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
// Adding the cells
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
// Customize the cells
cell.backgroundColor = .clear
cell.layer.cornerRadius = collectionView.contentSize.height / 2
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.black.cgColor
return cell
}
// The witdth and the height of the cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
}
// Padding to the cells
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
}
// Selecting a cell and navigating to another view controller
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let openViewController = self.viewControllerList[indexPath.row]
self.present(openViewController.viewController, animated: true)
}
}
FirstScreen.swift
class FirstScreen: TabBar {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
}
}
SecondScreen.swift
class SecondScreen: TabBar {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
}
ThirdScreen.swift
class ThirdScreen: TabBar {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let firstScreen = SecondScreen(collectionViewLayout: layout)
window?.rootViewController = firstScreen
return true
}
Теперь, когда я запускаю это, это происходит сбой, и причина, например, в том, что пользователь находится на FirstScreen.swift, и пользователь нажимает на первую ячейку, которая приведет пользователя к FirstScreen.swift, он будетне работает, есть ли способ решить эту проблему?Как отключить ячейку, когда пользователь находится на соответствующем контроллере представления.