如何解决导航标题视图中的UICollectionView对选择无响应
我在UINavigationItem的标题视图中有一个UICollectionView
作为菜单。我无法在集合视图中选择项目。如果我将UICollectionView放置在导航项之外并放入视图控制器,则一切都会按预期进行。
UIViewController
let menuView = HomeMenuView()
override func viewDidLoad() {
navigationItem.titleView = menuView
menuView.delegate = self
}
HomeMenuView(UICollectionView)
class HomeMenuView: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var sections = ["Recent","Following"]
var delegate: HomeMenuDelegate!
var selectedIndex = 0
var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
create()
}
func create() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
addSubview(collectionView)
collectionView.anchor(top: nil,left: nil,bottom: nil,right: nil,paddingTop: 0,paddingLeft: 0,paddingBottom: 0,paddingRight: 0,width: UIScreen.main.bounds.width/2,height: 30)
collectionView.center(x: centerXAnchor,y: centerYAnchor)
collectionView.backgroundColor = .clear
collectionView.isUserInteractionEnabled = true
collectionView.clipsToBounds = true
collectionView.allowsSelection = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(HomeMenuCell.self,forCellWithReuseIdentifier: "cellID")
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID",for: indexPath) as! HomeMenuCell
cell.title = sections[indexPath.row]
cell.isSelected = selectedIndex == indexPath.row
return cell
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
guard let currentCell = collectionView.cellForItem(at: IndexPath(row: selectedIndex,section: 0)) as? HomeMenuCell else { return }
guard let newCell = collectionView.cellForItem(at: indexPath) as? HomeMenuCell else { return }
currentCell.isSelected = false
newCell.isSelected = true
selectedIndex = indexPath.row
delegate.didSelectSection(at: indexPath.row)
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width)/CGFloat(sections.count),height: collectionView.frame.height)
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.layer.cornerRadius = collectionView.bounds.height/2
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
解决方法
无法选择您的收藏夹视图单元格,因为您的收藏夹视图超出其超级视图的范围。
按如下所示更改create()
函数的中间部分(您没有包括“约束帮助器”,因此我使用了标准约束语法):
addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: topAnchor,constant: 0.0),collectionView.leadingAnchor.constraint(equalTo: leadingAnchor,collectionView.trailingAnchor.constraint(equalTo: trailingAnchor,collectionView.bottomAnchor.constraint(equalTo: bottomAnchor,collectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.5),collectionView.heightAnchor.constraint(equalToConstant: 30.0),])
//collectionView.anchor(top: nil,left: nil,bottom: nil,right: nil,paddingTop: 0,paddingLeft: 0,paddingBottom: 0,paddingRight: 0,width: UIScreen.main.bounds.width/2,height: 30)
//collectionView.center(x: centerXAnchor,y: centerYAnchor)
collectionView.backgroundColor = .clear
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。