将 .xib 视图添加为子视图后,视图按钮不再可单击

View buttons are not anymore clickable after adding a .xib view as subview

提问人:Awais Shahid 提问时间:11/19/2019 更新时间:11/19/2019 访问量:169

问:

我有一个视图控制器,其中显示了下面的卡片。我使用 .xib 视图从视图控制器的底部滑动,因为在视图控制器的视图中添加 .xib 视图作为子视图后,即使 .xib 视图折叠,视图控制器按钮也不再可单击。“cardViewController” 是 .xib 视图的名称。

    var cardViewController:FrenchiasCardViewController!
func setupCard()
{
    // Setup starting and ending card height
    endCardHeight = self.view.frame.height * 0.9
    startCardHeight = self.view.frame.height * 0.08

    // Add Visual Effects View
    visualEffectView = UIVisualEffectView()
    visualEffectView.frame = self.view.frame
    self.view.addSubview(visualEffectView)

    // Add CardViewController xib to the bottom of the screen, clipping bounds so that the corners can be rounded
    cardViewController = FrenchiasCardViewController(nibName:"FrenchiasCardViewController", bundle:nil)
    self.view.addSubview(cardViewController.view)
    cardViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - startCardHeight, width: self.view.bounds.width, height: endCardHeight)
    cardViewController.view.clipsToBounds = true

    // Add tap and pan recognizers
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(NewAddressView.handleCardTap(recognzier:)))
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(NewAddressView.handleCardPan(recognizer:)))

    cardViewController.handleArea.addGestureRecognizer(tapGestureRecognizer)
    cardViewController.handleArea.addGestureRecognizer(panGestureRecognizer)
}

    let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
            switch state {
            case .expanded:
                // If expanding set popover y to the ending height and blur background
                self.cardViewController.view.frame.origin.y = self.view.frame.height - self.endCardHeight
                self.visualEffectView.effect = UIBlurEffect(style: .dark)

            case .collapsed:
                // If collapsed set popover y to the starting height and remove background blur
                self.cardViewController.view.frame.origin.y = self.view.frame.height - self.startCardHeight

                self.visualEffectView.effect = nil
            }
        }
Swift iPhone View UIVieviceController Xib

评论

0赞 shbedev 11/19/2019
尝试将按钮的超级视图 clipToBounds 设置为 true,并检查是否仍能看到这些按钮。这将帮助您了解超视图的框架是否存在问题。
0赞 Awais Shahid 11/19/2019
我的按钮是可见的,只是它们不会在点击时执行任何操作。
0赞 shbedev 11/19/2019
如果 clipToBounds 为 false,并且超级视图的大小较小,您将能够看到子视图,但无法与子视图交互。我过去遇到过这样的问题,使用 UI Hierarchy 调试器调试后,我发现 superview 高度为 0。所以我看到了按钮,但无法与它们交互。此外,我建议确保 isuserinteractionenabled 在 superview 上为 true。
0赞 Awais Shahid 11/20/2019
@Sam我尝试添加是否启用了用户交互,并且 cliptobounds true ,但仍然无法与按钮、视图上的文本视图进行交互,问题是我使用了 .xib 视图并折叠它,但随后添加 .xib 视图控制器视图不再能够交互
0赞 shbedev 11/20/2019
尝试使用 UI 层次结构调试器检查视图。

答: 暂无答案