IOS:带有子视图的UIView当zPosition视图发生变化时,按钮点击事件不会触发

Raj*_*aju 5 ios swift

我在我的应用程序中使用父视图控制器和子视图控制器.其中父视图控制器包含子视图作为按钮,zPosition值为2.

现在,我将子视图控制器添加到父级,如下所示,

    func addChildViewController(){

        let storyboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
        let childVC = storyboard.instantiateViewController(withIdentifier: "childVC") as! ChildViewController        
        addChildViewController(childVC)
        self.view.addSubview(childVC.view)
        childVC.didMove(toParentViewController: self)
     }
Run Code Online (Sandbox Code Playgroud)

按钮子视图在子视图控制器的顶部可见,但单击事件未触发.

注意:我没有在子视图控制器上添加按钮作为子视图,仅在父视图控制器上添加.

Wez*_*Wez 2

childVC 是否覆盖了按钮?- 添加childVC后,您需要确保其中包含按钮的子视图移到前面。

func addChildViewController(){
    let storyboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
    let childVC = storyboard.instantiateViewController(withIdentifier: "childVC") as! ChildViewController        
    addChildViewController(childVC)
    self.view.addSubview(childVC.view)
    childVC.didMove(toParentViewController: self)

    // Bring button subview to front
    self.view.bringSubviewToFront(SubViewWithButtonIn)

}
Run Code Online (Sandbox Code Playgroud)