2017-06-04 76 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。