gpt4 book ai didi

swift - 按钮在 Swift 中的 UICollectionView 上方不起作用

转载 作者:行者123 更新时间:2023-11-30 12:24:40 25 4
gpt4 key购买 nike

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

enter image description here

还有菜单:

enter image description here

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

我尝试调整图层的位置:

currentView.layer.zPosition = 2

但是它不起作用。您知道我可以做什么来启用 UICollection View 上方的按钮吗?

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

}

编辑

创建按钮( View 是菜单 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)
}

在 View 中加载:

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)

最佳答案

这很简单......

我刚刚移动了 collectionViewLaunch() 函数来添加菜单的 subview 。

确实,在这种情况下,菜单 View 位于 collectionView 上方。

关于swift - 按钮在 Swift 中的 UICollectionView 上方不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44352473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com