简体   繁体   English

CollectionView Touch 现在放在 tabBar 顶部时可以工作 Swift

[英]CollectionView Touch is now working when placed on top of tabBar Swift

I have added a collectionView on top of a UITabBar but its touch is not working.我在 UITabBar 上添加了一个 collectionView,但它的触摸不起作用。 The screeshot for the tabBar and collectionView tabBar 和 collectionView 的截图

The code is attached below, I want the collectionView to be touchable.代码附在下面,我希望 collectionView 是可触摸的。 Here quickAccessView is the UIView that contains the collectionView.这里quickAccessView就是包含collectionView的UIView。 For constraints I'm using snapKit对于约束,我正在使用 snapKit

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.tabBar.bringSubviewToFront(quickAccessView)
    }

    private func setupQuickAccessView(){
        print("this is tabBar's height", self.tabBar.frame.size.height)
        self.tabBar.frame.size.height = 150
        print("this is new tabBar's height", self.tabBar.frame.size.height)
        self.tabBar.addSubview(quickAccessView)
        quickAccessView.clipsToBounds = true
    }
    private func addQuickAccessViewConstraints(){
        quickAccessView.snp.makeConstraints { make in
            make.right.left.equalTo(self.tabBar.safeAreaLayoutGuide)
            make.height.equalTo(76)
            make.bottom.equalTo(self.tabBar.snp.bottom).offset(-80)
        }
    }

I think what may be happening here is that since you've added quickAccessView as tab bar's subview, it is not accepting touches.我认为这里可能发生的情况是,由于您已将quickAccessView添加为选项卡栏的子视图,因此它不接受触摸。 This would be so because the tabbar's hist test will fail in this scenario.之所以如此,是因为在这种情况下标签栏的 hist 测试将失败。

To get around this, instead of using a UITabBar, subclass UITabBar, let us call it ToastyTabBar for reference.为了解决这个问题,我们不使用 UITabBar,子类 UITabBar,而是将其ToastyTabBar以供参考。 See the code below:请参阅下面的代码:

class ToastyTabBar: UITabBar {
    override public func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        // if `quickAccessView` is visible, then convert `point` to its coordinate-system
        // and check if it is within its bounds; if it is, then ask `quickAccessView`
        // to perform the hit-test; you may skip the `isHidden` check, in-case this view
        // is always present in your app; I'm assuming based on your screenshot that
        // the user can dismiss / hide the `quickAccessView` using the cross icon

        if !quickAccessView.isHidden {
            // Convert the point to the target view's coordinate system.
            // The target view isn't necessarily the immediate subview

            let targetPoint = quickAccessView.convert(point, from: self)
            
            if quickAccessView.bounds.contains(targetPoint) {

                // The target view may have its view hierarchy, so call its
                // hitTest method to return the right hit-test view
                return quickAccessView.hitTest(targetPoint, with: event)
            }
        }
        
        // else execute tabbar's default implementation
        return super.hitTest(point, with: event)
    }
}

Set this as the class of your tabBar (both in the storyboard and the swift file), and then see if that solves it for you.将其设置为 tabBar 的tabBar (在 storyboard 和 swift 文件中),然后查看是否可以为您解决问题。 You'll have to figure out a way to make quickAccessView accessible to the tabbar for the hit test check.您必须想出一种方法,使标签栏可以访问quickAccessView以进行命中测试检查。 I haven't advised on that above because I'm not familiar with your class hierarchy, and how and where you set stuff up, but this should be trivial.我没有在上面提出建议,因为我不熟悉您的 class 层次结构,以及您设置内容的方式和位置,但这应该是微不足道的。

If this solves it for you, please consider marking this as the answer, and if it does not then please share a little more info here about where you're having the problem.如果这为您解决了问题,请考虑将此标记为答案,如果没有,请在此处分享更多有关您遇到问题的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM