【问题标题】:UICollectionView in navigation title view is unresponsive to selection导航标题视图中的 UICollectionView 对选择没有响应
【发布时间】:2021-02-15 12:23:41
【问题描述】:

我在 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")
}
}

【问题讨论】:

    标签: ios swift uicollectionview uinavigationcontroller


    【解决方案1】:

    无法选择您的集合视图单元格,因为您的集合视图超出了其超级视图的范围。

    如下更改create() func 的中间部分(您没有包含“约束助手”,所以我使用标准约束语法):

        addSubview(collectionView)
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
    
            collectionView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0),
            collectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
            collectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),
            collectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0),
    
            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
    

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2021-05-11
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2021-07-26
      相关资源
      最近更新 更多