iOS : UIView with subview Button click event not firing when zPosition of view changes

标签 ios swift

我在我的应用程序中使用父 View Controller 和 subview Controller 。其中父 View Controller 包含 subview 作为 zPosition 值为 2 的按钮。

现在,我将 subview Controller 添加到父 View Controller ,如下所示,

    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)
     }

按钮 subview 在 subview Controller 顶部可见,但未触发点击事件。

注意:我没有在 subview Controller 上添加按钮作为 subview ,只是在父 View Controller 上。

最佳答案

childVC 是否覆盖了按钮? - 您需要确保在添加 childVC 后将带有按钮的 subview 移动到前面。

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)

}

关于iOS : UIView with subview Button click event not firing when zPosition of view changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668513/

相关文章:

swift - 有没有一种简单的方法可以识别 Watch Kit 中文本的颜色 - Swift

ios - 如何在 swift Xcode Playground 中获取用户输入

ios - Firebase 是否在 iOS 应用程序中使用钥匙串(keychain)?

ios - Swift - UIView 缩放

ios - sortedArrayUsingComparators 不按升序对数字进行排序

ios - 推送 viewController 并保持 webView 状态 ios

ios - 检查节点在屏幕上是否可见

swift - ARKit 中渲染器和 session 方法的区别

ios - 找不到图书馆 |链接器命令失败

ios - 在 CrEdit 中列出来自 Google Drive 的所有文件和文件夹