简体   繁体   English

UIView SubView UIButton不能响应触摸

[英]UIView SubView UIButton not responding to touch

I have an UIButton located inside of an UIView subview. 我有一个位于UIView子视图内的UIButton。 The view appears fine but the button is not responding to touch. 视图看起来很好,但是按钮对触摸没有响应。 The subviews are two separate ViewControllers embedded into the container views. 子视图是嵌入到容器视图中的两个单独的ViewController。

On the storyboard all views, controllers, and buttons have user interaction enabled. 在情节提要上,所有视图,控制器和按钮均启用了用户交互。 I have also enabled user interaction for the subviews programmatically. 我还以编程方式为子视图启用了用户交互。

Here is the code: 这是代码:

@IBOutlet var showtimeView: UIView!
@IBOutlet var reviewView: UIView!
var viewTwo: UIView!
var viewThree: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    viewTwo = ShowtimesView().view
    showtimeView.addSubview(viewTwo)
    viewThree = ReviewsView().view
    reviewView.addSubview(viewThree)
    showtimeView.isUserInteractionEnabled = true
    reviewView.isUserInteractionEnabled = true
    viewTwo.isUserInteractionEnabled = true
    viewThree.isUserInteractionEnabled = true


}

@IBAction func segmentChange(_ sender: Any) {

    switch(segment.selectedSegmentIndex) {

    case 0:

        detailsView.isHidden = false
        showtimeView.isHidden = true
        reviewView.isHidden = true

        break

    case 1:

        detailsView.isHidden = true
        showtimeView.isHidden = false
        reviewView.isHidden = true

        break

    case 2:

        detailsView.isHidden = true
        showtimeView.isHidden = true
        reviewView.isHidden = false

        break

    default:
        break

    }

}

What else could be preventing the UIButton located inside of the subview from responding to touch? 还有什么可能阻止位于子视图内部的UIButton响应触摸?

EDIT: I changed the color of the container to check if it was covering the viewcontroller. 编辑:我更改了容器的颜色,以检查它是否覆盖了viewcontroller。 The container did not show up in front of the subview viewcontroller. 该容器未显示在子视图ViewController的前面。 The subviews still do not respond to touch. 子视图仍然无法响应触摸。

If there is another view on top of the button, such as a container view and the view within that container view (and any other view of that container view that covers the button), then touch events won't make it to that button. 如果按钮上方还有另一个视图,例如容器视图该容器视图内的视图(以及该容器视图中覆盖该按钮的任何其他视图),那么触摸事件将不会到达该按钮。

To compensate, have the container view (that's covering your button) and the view within it both derive from the following view: 作为补偿,使容器视图(覆盖按钮)及其内的视图均从以下视图派生:

class PassthroughView: UIView
{
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
    {
        let view = super.hitTest(point, with: event)
        return view == self ? nil : view
    }
}

You can make the change from UIView to PassthroughView for those two views directly in the storyboard, and just toss the above class somewhere (I prefer it to be in its own file). 您可以直接在情节提要中将这两个视图从UIView更改为PassthroughView,而只是将上述类扔到某个地方(我希望将其放在自己的文件中)。

The solution was to delete the code in viewDidLoad. 解决的办法是删除viewDidLoad中的代码。 Since the views are already embedded into the container views through storyboard segues they don't need to be created as subviews. 由于视图已经通过情节提要序列嵌入到容器视图中,因此不需要将其创建为子视图。

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

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