I'm trying to show a menu when I click on the Navigation Bar (Like Instagram when to use several accounts). So, this is what I have:
And with the menu:
The list into the menu is buttons which are added in code. My problem is that only the first button is working. After several tests, it's working because this button is above the searchbar. But if a button is above the UICollectionView, it's not working any more.
I tried to play with the position of the layer :
currentView.layer.zPosition = 2
But it's not working. Do you have any idea about what I could do to enable button above the UICollection View ?
This is the code to launch the CollectionView:
func collectionViewLaunch() {
// Layout of collectionView
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: self.view.frame.size.width * 0.47, height: self.view.frame.size.width * 0.47)
layout.scrollDirection = UICollectionViewScrollDirection.vertical
let frame = CGRect(x: 0, y: 44, width: self.view.frame.size.width, height: self.view.frame.size.height - self.tabBarController!.tabBar.frame.size.height - self.navigationController!.navigationBar.frame.size.height - 62 )
layout.sectionInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
collectionView.backgroundColor = UIColor.white
self.view.addSubview(collectionView)
// Define cell for collectionView
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Call function to load posts
loadPosts()
}
Creation of buttons (the view is the menu view) :
func menuTapped(_ list: Array<String>, view: UIView, xCoord: CGFloat, yCoord: CGFloat, buttonWidth: CGFloat, buttonHeight: CGFloat){
var yCoord = yCoord
var itemCount = 0
for i in 0 ..< list.count {
itemCount = i
let listButton = UIButton(type: .custom)
listButton.frame = CGRect(x: xCoord, y: (yCoord), width: buttonWidth, height: 45)
// istButton.tag = itemCount
listButton.addTarget(self, action: #selector(self.menuButtonTapped(_:)), for: .touchUpInside)
listButton.setTitle("\(list[i])", for: .normal)
listButton.setTitleColor(UIColor(red: 109 / 255, green: 109 / 255, blue: 109 / 255, alpha: 1), for: .normal)
listButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFontWeightThin)
listButton.tag = itemCount
// Add Buttons in the Scroll View
yCoord += buttonHeight
view.addSubview(listButton)
}
}
func menuButtonTapped(_ sender: Any){
let button = sender as! UIButton
// let tmpButton = self.view.viewWithTag(button.tag) as? UIButton
print(button.tag)
}
In the view did load:
sportMenuView = UIView(frame: CGRect(x: 0, y: CGFloat(-menuSport.count * 45), width: self.view.frame.size.width, height: CGFloat(menuSport.count * 45)))
sportMenuView.backgroundColor = UIColor.white
self.view.addSubview(sportMenuView)
sportMenuView.layer.zPosition = 2
menuTapped(menuSport, view: sportMenuView, xCoord: xCoord, yCoord: yCoord, buttonWidth: buttonWidth, buttonHeight: buttonHeight)
It was pretty simple ...
I just moved the collectionViewLaunch() function before to add the menu's subview.
Indeed, in that case, the menu's view is above the collectionView.