我在我的应用程序中使用父 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/