addsubview视图不可点击

时间:2016-07-19 07:42:11

标签: swift xcode

我添加了这样的视图

AffairesViewController

func clickChangeColor(nomAffaire : String, idEcoute : String, config : ConfigDto ){

    let changeColorViewController = ChangeColorViewController()
    changeColorViewController.showCustomAlertInView(self.view, message: "", button: "OK")
}

ChangeColorViewController

 func showCustomAlertInView(targetView : UIView ,message : String, button : String)
    {
        targetView.addSubview(self.view)
    }

但是视图ChangeColorViewController不可点击,当我点击我添加的视图时,只点击后面

在图像中我们可以看到结果,问题是出现的视图(蓝色)不可点击

https://jsfiddle.net/IsaacAdni/q56zkf2t/

3 个答案:

答案 0 :(得分:3)

@tamtoum我想我知道你想要什么。所以首先我要解释我的理解。你有一个名为ChangeColorViewController的视图控制器,其中有一个应该在AffairesViewController上显示的视图(一个小视图)。现在我无法理解为什么你从AffairesViewController调用一个方法,然后在该方法中添加ChangeColorViewController。我有办法解决你的问题。

无需调用任何方法来添加子视图,而是为您在方法中传递的数据创建属性。 Handel表示ChangeColorViewController的viewDidiLoad()中的数据。在代码中进行以下更改:

func clickChangeColor(nomAffaire : String, idEcoute : String, config : ConfigDto ){

    let changeColorVC = self.storyboard?.instantiateViewControllerWithIdentifier("StoryBoardIdentifier") as! ChangeColorViewController

    /* pass data via properties here for eg.
       changeColorVC.message = "YourMessage"
       changeColorVC.button = "YourButtonText"
    */

    self.addChildViewController(changeColorVC)
    self.view.addSubView(changeColorVC.view)

}

这个答案是根据我迄今所理解的。如果你有任何疑问,那么你可以在这里问。

希望这会对你有所帮助:)。

答案 1 :(得分:1)

如果要添加的子视图超出了超级视图的范围,则在子视图中不会检测到任何触摸。检查这个

答案 2 :(得分:0)

添加视图时,默认情况下会禁用userInteraction。只需添加view.userInteractionEnabled = true

即可