2017-06-04 35 views
0

我试图显示菜单,当我在导航栏集团公司(像Instagram的时候使用多个账户)。 所以,这就是我:斯威夫特 - 按钮不起作用上述UICollectionView

enter image description here

而且随着菜单:enter image description here

列表进入菜单是在代码中添加按钮。 我的问题是,只有第一个按钮工作......经过多次试验,它的工作,因为这个按钮是搜索栏上方。但是如果一个按钮位于UICollectionView之上,它就不再工作了。

我试图与层的postiion玩:

currentView.layer.zPosition = 2 

但它不工作...你有什么我可以做,以使上述UICollection视图按钮的任何想法?

这是启动的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() 

    } 

非常感谢你提前

编辑:

创建按钮(视图是菜单视图):

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) 
    } 

在视图中加载:

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) 
+1

你在哪里添加按钮?请分享代码找出问题所在。 –

+0

它被修改了,如果你有任何问题,请随意。谢谢 ! – KevinB

+1

我把你的代码和所有的按钮一起打包成一个简单的应用程序,即使是上面的collectionView。你在细胞中做过任何不寻常的触摸窃取(手势识别器?)吗?也许你可以在显示菜单时尝试'collectionView.isUserInteractionEnabled = false'? – Magnas

回答

0

这是很简单的......

我刚搬来的collectionViewLaunch()函数之前添加菜单的子视图。

事实上,在这种情况下,菜单的看法是上面的CollectionView。