Button在Swift中的UICollectionView上面不起作用

时间:2017-06-04 09:11:11

标签: swift button swift3 menu uicollectionview

我在点击导航栏时尝试显示菜单(就像Instagram使用多个帐户时一样)。所以,这就是我所拥有的:

enter image description here

使用菜单:

enter image description here

菜单中的列表是按代码添加的按钮。我的问题是只有第一个按钮正常工作。经过多次测试后,它才能正常工作,因为此按钮位于搜索栏上方。但是如果按钮位于UICollectionView上方,则它不再起作用。

我尝试使用图层的位置:

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 :(得分:0)

这很简单......

我之前只是移动了collectionViewLaunch()函数来添加菜单的子视图。

实际上,在这种情况下,菜单的视图位于collectionView。

之上